XSLT元素通过连接排序

时间:2012-05-11 20:00:15

标签: xml xslt

希望通过加入fsxml/formula/ingrrow itemcode = fsxml/item(属性objkey)并将项目中名为erpplantnr_11erpplantnr_30的元素加入以排序以下XML在每个attribute10的最后一个ingrrow元素之后移动。

提前感谢您提供的任何帮助!

(简化并提供首次XSLT尝试) (简化示例 - 想要在属性objkey与itemcode元素匹配的attribute1元素之后移动erpplantnr_11元素)

寻求有关如何创建“加入”的帮助。再次感谢。

(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>

结果XML - 需要将两个项目对象键属性与其ingrrow项目代码匹配,并使输出xml列表在attribute1元素之后的erpplantnr_11元素:

<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>

原始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>

1 个答案:

答案 0 :(得分:0)

我仍然很难理解你究竟需要什么,但我希望这会帮助你开始。

我假设您要复制输入XML,而不是复制itemingrrow一起,而是想从相应的erpplantnr_中取出item个元素。 {1}}并将它们放在attribute的最后一个ingrrow子节点之后。

虽然我们可以在XPath谓词中进行内联查找,但最好将xsl:key and fn:key()看作是表达查找的更好方法:

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

这个将item“索引”所有@objectkey。接下来,让我们将身份转换放在那里,就像你拥有它一样:

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

接下来,让我们沉默item,因为我们说我们只需要提取一些匹配的item来显示在其他地方:

<xsl:template match="item"/>

最后,让我们截取最后一个attribute节点并在其后面注入匹配的erpplantnr_节点:

<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>

这里我们复制最后一个attribute节点,然后使用该键查找匹配的item节点,我们按照它们在文档中出现的顺序列举所有erpplantnr_节点。

我不确定你想要那些选择器是多么通用或具体,所以我在中间选了一个位置。

当我在输入文档上运行此转换时,我得到了它的副本而没有item个节点,但在每个erpplantnr_节点之后复制了相应的attribute10 {1}}。

希望它有所帮助。

P.S。完整转换列表以防万一:

ingrrow