0

Would like to sort the following XML by joining the fsxml/formula/ingrrow where itemcode = fsxml/item (attribute objkey) and having the elements named erpplantnr_11 through erpplantnr_30 from item moved after the last attribute10 element for each ingrrow.

Thanks in advance for any help that can be provided!

(Simplifying and providing first XSLT attempt) (Simplified example - want to move erpplantnr_11 element after attribute1 element where the attribute objkey matches the itemcode element)

Looking for help on how to create the "join". Thanks again.

(XSLT):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ms="urn:schemas-microsoft-com:xslt" 

xmlns:fsxml="http://www.FormationSystems.com" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="formula">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="ingrrow"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="ingrrow | item">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="itemcode"/>
    <xsl:apply-templates select="attribute1"/>
    <xsl:apply-templates select="erpplantnr_11"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="fsxml/fsxml">
  <xsl:apply-templates select="item"/>
</xsl:template>

</xsl:stylesheet>

Resultant XML - Need to match the two item objectkey attributes with their ingrrow itemcodes and have the output xml list the erpplantnr_11 element after the attribute1 element:

<fsxml>
  <formula maxcol="51" keycount="2" fmt="A" dtlcodes="HEADER\INGR" objectkey="NOR000133\0002" filteredfields="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ingrrow detail="1" fmt="X">
      <itemcode>NOR60506</itemcode> 
      <attribute1>000000000050051116</attribute1> 
    </ingrrow>
    <ingrrow detail="1" fmt="X">
      <itemcode>7K015</itemcode> 
      <attribute1>000000000000800479</attribute1> 
    </ingrrow>
  </formula>
  <item maxcol="35" keycount="1" fmt="A" dtlcodes="HEADER" objectkey="NOR60506" filteredfields="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <erpplantnr_11>121212;343434</erpplantnr_11> 
  </item>
  <item maxcol="35" keycount="1" fmt="A" dtlcodes="HEADER" objectkey="7K015" filteredfields="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <erpplantnr_11>123456</erpplantnr_11> 
  </item>
</fsxml>

Original XML:

<fsxml>
  <formula maxcol="51" keycount="2" fmt="A" dtlcodes="HEADER\INGR" objectkey="NOR000133\0002" filteredfields="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <keycode>NOR000133</keycode> 
      <keycode2>0002</keycode2> 
      <description>48782 ALABASTER WHITE MB9960 NA</description> 
      <formulaalias /> 
      <alternateformula /> 
      <timedate xsi:nil="true" /> 
      <productcode /> 
      <productdescr /> 
      <reportcode /> 
      <reportregion /> 
      <alias_code1 /> 
      <alias_code2 /> 
      <alias_code3 /> 
      <alias_code4 /> 
      <alias_code5 /> 
      <alias_code6 /> 
      <alias_code7 /> 
      <alias_code8 /> 
      <hseapacdoc /> 
      <hseemeadoc /> 
      <hsenasadoc /> 
      <wau_local_designee /> 
      <apac1_local_designee /> 
      <apac2_local_designee /> 
      <apc_local_designee /> 
      <emea_local_designee /> 
      <na_local_designee>PREST</na_local_designee> 
      <sa_local_designee /> 
      <ras_sent>Yes</ras_sent> 
      <erp_waunr /> 
      <erp_apac1nr /> 
      <erp_apac2nr /> 
      <erp_apcnr /> 
      <erp_emeanr /> 
      <erp_nornr /> 
      <erp_sanr /> 
      <crfc_code>NOR000133</crfc_code> 
      <orig_desc /> 
      <erpglobal_1 /> 
      <erpglobal_2 /> 
      <erpglobal_3 /> 
      <uomcode>KG</uomcode> 
      <itemcode>NOR000133</itemcode> 
      <yield>74.8956541853986</yield> 
      <yieldpct>100</yieldpct> 
      <phantomind>0</phantomind> 
      <primaryformulaind>1</primaryformulaind> 
      <processyield>100</processyield> 
      <projectcode /> 
      <labbookcode /> 
      <labbookpage /> 
      <ownercode>ARFTL</ownercode> 
      <groupcode>GROUP_1</groupcode> 
      <statusind>291</statusind> 
      <approvalcode /> 
      <holdcode /> 
      <workcode /> 
      <workcode2 /> 
      <formulatorcode>ARFTL</formulatorcode> 
      <yieldcalcind>1</yieldcalcind> 
      <customer>RO_NOR</customer> 
      <comment /> 
      <datemodified>2012-05-11T14:55:37</datemodified> 
      <class>PRODUCT</class> 
      <typeind>0</typeind> 
      <parentfid>42871</parentfid> 
      <mfgitemmaster>NOR000133\0002</mfgitemmaster> 
      <calcmode>0</calcmode> 
      <formulaid>728067</formulaid> 
      <adjustparam /> 
      <materialchange>1</materialchange> 
      <judgement /> 
    - <ingrrow detail="1" fmt="X">
        <lineid>1</lineid> 
        <itemcode>NOR60506</itemcode> 
        <quantity>66.6877872652848</quantity> 
        <uomcode>KG</uomcode> 
        <description>KV1348 KV1348 White Dispersion</description> 
        <materialpct>89.0409303324918</materialpct> 
        <linebreakcode /> 
        <scaleind>0</scaleind> 
        <subformulaind>0</subformulaind> 
        <formulaid>0</formulaid> 
        <componentind>1</componentind> 
        <instruction /> 
        <substituteind>0</substituteind> 
        <formulacode>NOR60506\0006</formulacode> 
        <status>401</status> 
        <class /> 
        <decdigit>0</decdigit> 
        <cas /> 
        <commcode /> 
        <aliascode1 /> 
        <aliascode2 /> 
        <aliascode3 /> 
        <aliascode4 /> 
        <activequantity>0</activequantity> 
        <relqtypct>0</relqtypct> 
        <attribute1>000000000050051116</attribute1> 
        <attribute2 /> 
        <attribute3 /> 
        <attribute4 /> 
        <attribute5 /> 
        <instrucflag>1</instrucflag> 
        <paramcode /> 
        <pvalue xsi:nil="true" /> 
        <lotcode /> 
        <aliascode5 /> 
        <aliascode6 /> 
        <aliascode7>APC 56% 5T070/3Z145 (KV1348) / P:393367</aliascode7> 
        <aliascode8 /> 
        <sectionname /> 
        <sectiontype>0</sectiontype> 
        <adjustind>0</adjustind> 
        <attribute6 /> 
        <attribute7 /> 
        <attribute8 /> 
        <attribute9 /> 
        <attribute10 /> 
      </ingrrow>
    - <ingrrow detail="1" fmt="X">
        <lineid>2</lineid> 
        <itemcode>7K015</itemcode> 
        <quantity>8.20786692011385</quantity> 
        <uomcode>KG</uomcode> 
        <description>METHYLAMYLKETONE</description> 
        <materialpct>10.9590696675082</materialpct> 
        <linebreakcode /> 
        <scaleind>0</scaleind> 
        <subformulaind>0</subformulaind> 
        <formulaid>0</formulaid> 
        <componentind>8</componentind> 
        <instruction /> 
        <substituteind>0</substituteind> 
        <status>401</status> 
        <class /> 
        <decdigit>0</decdigit> 
        <cas>110-43-0</cas> 
        <commcode /> 
        <aliascode1>S194</aliascode1> 
        <aliascode2>METHYL AMYL KETONE</aliascode2> 
        <aliascode3 /> 
        <aliascode4>METHYLAMYLKETONE</aliascode4> 
        <activequantity>0</activequantity> 
        <relqtypct>0</relqtypct> 
        <attribute1>000000000000800479</attribute1> 
        <attribute2 /> 
        <attribute3 /> 
        <attribute4 /> 
        <attribute5 /> 
        <instrucflag>1</instrucflag> 
        <paramcode /> 
        <pvalue xsi:nil="true" /> 
        <lotcode /> 
        <aliascode5>METHYLAMYLKETONE</aliascode5> 
        <aliascode6>Methylamylketon 165,5 KG</aliascode6> 
        <aliascode7>METHYL N-AMYL KETONE (Bulk)</aliascode7> 
        <aliascode8 /> 
        <sectionname /> 
        <sectiontype>0</sectiontype> 
        <adjustind>0</adjustind> 
        <attribute6 /> 
        <attribute7 /> 
        <attribute8 /> 
        <attribute9 /> 
        <attribute10 /> 
      </ingrrow>
    </formula>
  - <fsxml>
    - <item maxcol="35" keycount="1" fmt="A" dtlcodes="HEADER" objectkey="NOR60506" filteredfields="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <keycode>NOR60506</keycode> 
        <description>KV1348 White Dispersion</description> 
        <rmremark /> 
        <ras_sent /> 
        <erp_waunr /> 
        <erp_apac1nr /> 
        <erp_apac2nr /> 
        <erp_apcnr /> 
        <erp_emeanr /> 
        <erp_nornr>000000000050051116</erp_nornr> 
        <erp_sanr /> 
        <erpglobal_1 /> 
        <erpglobal_2 /> 
        <erpglobal_3 /> 
        <erpplantnr_11>121212;343434</erpplantnr_11> 
        <erpplantnr_12 /> 
        <erpplantnr_13 /> 
        <erpplantnr_14 /> 
        <erpplantnr_15 /> 
        <erpplantnr_16 /> 
        <erpplantnr_17 /> 
        <erpplantnr_18 /> 
        <erpplantnr_19 /> 
        <erpplantnr_20 /> 
        <erpplantnr_21 /> 
        <erpplantnr_22 /> 
        <erpplantnr_23 /> 
        <erpplantnr_24 /> 
        <erpplantnr_25 /> 
        <erpplantnr_26 /> 
        <erpplantnr_27 /> 
        <erpplantnr_28 /> 
        <erpplantnr_29 /> 
        <erpplantnr_30 /> 
        <uomcode>KG</uomcode> 
        <calcind>0</calcind> 
        <scaleind>0</scaleind> 
        <statusind>401</statusind> 
        <approvalcode /> 
        <class /> 
        <componentind>1</componentind> 
        <linebreakcode /> 
        <holdcode /> 
        <formulacode>NOR60506</formulacode> 
        <version>0006</version> 
        <cas /> 
        <commcode /> 
        <aliascode1 /> 
        <aliascode2 /> 
        <aliascode3 /> 
        <aliascode4 /> 
        <aliascode5 /> 
        <aliascode6 /> 
        <aliascode7>APC 56% 5T070/3Z145 (KV1348) / P:393367</aliascode7> 
        <aliascode8 /> 
        <threshold>0</threshold> 
        <datemodified>2012-04-28T19:50:12</datemodified> 
        <ftypeind>0</ftypeind> 
        <judgement /> 
      </item>
      - <item maxcol="35" keycount="1" fmt="A" dtlcodes="HEADER" objectkey="7K015" filteredfields="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <keycode>7K015</keycode> 
        <description>METHYLAMYLKETONE</description> 
        <rmremark /> 
        <ras_sent /> 
        <erp_waunr>S194</erp_waunr> 
        <erp_apac1nr>00000000000007K015</erp_apac1nr> 
        <erp_apac2nr /> 
        <erp_apcnr>7K015</erp_apcnr> 
        <erp_emeanr>000000000000800479</erp_emeanr> 
        <erp_nornr>000000000000800479</erp_nornr> 
        <erp_sanr /> 
        <erpglobal_1 /> 
        <erpglobal_2 /> 
        <erpglobal_3 /> 
        <erpplantnr_11>123456</erpplantnr_11> 
        <erpplantnr_12>234567</erpplantnr_12> 
        <erpplantnr_13>345678</erpplantnr_13> 
        <erpplantnr_14>456789</erpplantnr_14> 
        <erpplantnr_15>567890</erpplantnr_15> 
        <erpplantnr_16>678901</erpplantnr_16> 
        <erpplantnr_17>789012</erpplantnr_17> 
        <erpplantnr_18>890123</erpplantnr_18> 
        <erpplantnr_19>901234</erpplantnr_19> 
        <erpplantnr_20>012345</erpplantnr_20> 
        <erpplantnr_21>654321</erpplantnr_21> 
        <erpplantnr_22>765432</erpplantnr_22> 
        <erpplantnr_23>876543</erpplantnr_23> 
        <erpplantnr_24>987654</erpplantnr_24> 
        <erpplantnr_25>098765</erpplantnr_25> 
        <erpplantnr_26>109876</erpplantnr_26> 
        <erpplantnr_27>210987</erpplantnr_27> 
        <erpplantnr_28>321098</erpplantnr_28> 
        <erpplantnr_29>432109</erpplantnr_29> 
        <erpplantnr_30>543210</erpplantnr_30> 
        <uomcode>KG</uomcode> 
        <calcind>0</calcind> 
        <scaleind>0</scaleind> 
        <statusind>401</statusind> 
        <approvalcode /> 
        <class /> 
        <componentind>8</componentind> 
        <linebreakcode /> 
        <holdcode /> 
        <formulacode /> 
        <version /> 
        <cas>110-43-0</cas> 
        <commcode /> 
        <aliascode1>S194</aliascode1> 
        <aliascode2>METHYL AMYL KETONE</aliascode2> 
        <aliascode3 /> 
        <aliascode4>METHYLAMYLKETONE</aliascode4> 
        <aliascode5>METHYLAMYLKETONE</aliascode5> 
        <aliascode6>Methylamylketon 165,5 KG</aliascode6> 
        <aliascode7>METHYL N-AMYL KETONE (Bulk)</aliascode7> 
        <aliascode8 /> 
        <threshold>0</threshold> 
        <datemodified>2012-04-24T22:52:56</datemodified> 
        <ftypeind>0</ftypeind> 
        <judgement /> 
      </item>
    </fsxml>
  </fsxml>
4

1 回答 1

0

It's still kind of hard for me to understand what exactly you need but I hope this will help you get going.

I assumed you want to copy the input XML but instead of copying over the items alongside the ingrrows, you rather want to take out those erpplantnr_ elements from the corresponding item and put them right after the last attribute child node of the ingrrow.

While we can do an inline lookup in our XPath predicates it's a good idea to look at the xsl:key and fn:key() as a better way to express a lookup:

<xsl:key name="items" match="/fsxml/fsxml/item" use="@objectkey"/>

This one will "index" all items by the @objectkey. Next, let's put the identity transform in there, just like you had it:

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

Next, let's silent the item since we said we only need to pull out something of a matching item to be displayed elsewhere:

<xsl:template match="item"/>

Finally, let's intercept that last attribute node and inject the matching erpplantnr_ nodes right after it:

<xsl:template match="ingrrow/*[contains(local-name(), 'attribute')][last()]">
    <xsl:copy-of select="."/>
    <xsl:apply-templates select="key('items', current()/parent::*/itemcode)/*[contains(local-name(), 'erpplantnr_')]"/>
</xsl:template>

Here we copy that last attribute node and then use the key to lookup a matching item node from which we enumerate all erpplantnr_ in the order of their occurrence in the document.

I am not sure how generic or specific you wanted those selectors to be so I picked a place in the middle.

When I run this transform on your input document I get a copy of it in return without the item nodes but with the corresponding erpplantnr_ copied right after that attribute10 node in each ingrrow.

Hope it helps.

p.s. full transform listing just in case:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ms="urn:schemas-microsoft-com:xslt" 
    xmlns:fsxml="http://www.FormationSystems.com" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:key name="items" match="/fsxml/fsxml/item" use="@objectkey"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="item"/>

    <xsl:template match="ingrrow/*[contains(local-name(), 'attribute')][last()]">
        <xsl:copy-of select="."/>
        <xsl:apply-templates select="key('items', current()/parent::*/itemcode)/*[contains(local-name(), 'erpplantnr_')]"/>
    </xsl:template>

</xsl:stylesheet>
于 2012-05-14T13:40:02.167 回答