根据起始元素对兄弟姐妹进行分组

时间:2014-01-14 11:44:08

标签: xml xslt-1.0

输入xml包含不同货件的多个重复记录。 记录1,2和3属于一个货件,记录1,2和3属于另一个货件。 输出应该像输出xml一样。每批货物都会转换为Root-Element

输入:

 <?xml version = '1.0' encoding = 'UTF-8'?>
    <Root-Element>

          <RECORD1>
             <C2>500000000222725718846C </C2>
             <C3>3014 </C3>
             <C4> DELVRY02  </C4>
          </RECORD1>
          <RECORD2>
             <C2> 50000000022272571880</C2>
             <C3>000010000000200</C3>
             <C4>9659666341109001 </C4>
           </RECORD2>
          <RECORD3>
             <C2> 50000000022272</C2>
             <C3>5718800000</C3>
             <C4>200000103002</C4>
          </RECORD3>


         <RECORD1>
             <C2>5000000002227ABCD </C2>
             <C3>3014 </C3>
             <C4> DELVRY02  </C4>
          </RECORD1>
          <RECORD2>
             <C2> 50000000022272571880</C2>
             <C3>000010000000200</C3>
             <C4>9659666341109001 </C4>
           </RECORD2>
          <RECORD3>
             <C2> 50000000022272</C2>
             <C3>5718800000</C3>
             <C4>200000103002</C4>
          </RECORD3>

    </Root-Element>

输出:

   <?xml version="1.0"?>
<Root-Element>
<shipment>
<RECORD1>
<C2>500000000222725718846C </C2>
<C3>3014 </C3>
<C4> DELVRY02 </C4>
</RECORD1>
<RECORD2>
<C2> 50000000022272571880</C2>
<C3>000010000000200</C3>
<C4>9659666341109001 </C4>
</RECORD2>
<RECORD3>
<C2> 50000000022272</C2>
<C3>5718800000</C3>
<C4>200000103002</C4>
</RECORD3>
</shipment>
<shipment>
<RECORD1>
<C2>5000000002227ABCD </C2>
<C3>3014 </C3>
<C4> DELVRY02 </C4>
</RECORD1>
<RECORD2>
<C2> 50000000022272571880</C2>
<C3>000010000000200</C3>
<C4>9659666341109001 </C4>
</RECORD2>
<RECORD3>
<C2> 50000000022272</C2>
<C3>5718800000</C3>
<C4>200000103002</C4>
</RECORD3>
</shipment>
</Root-Element>

我在xslt下面尝试了这个,但它给出了一个问题。

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
  <xsl:key name="items" match="Root-Element/*"
           use="generate-id(preceding-sibling::RECORD1[1])"/>
  <xsl:template match="/Root-Element">
    <Root-Element>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="RECORD1"/>
    </Root-Element>
  </xsl:template>
  <xsl:template match="RECORD1">
    <shipment>
      <xsl:copy-of select="."/>
      <xsl:copy-of select="key('items', generate-id())"/>
    </shipment>
  </xsl:template>

</xsl:stylesheet>

输出上面的xslt:

<?xml version="1.0"?>
<Root-Element>
<shipment>
<RECORD1>
<C2>500000000222725718846C </C2>
<C3>3014 </C3>
<C4> DELVRY02 </C4>
</RECORD1>
<RECORD2>
<C2> 50000000022272571880</C2>
<C3>000010000000200</C3>
<C4>9659666341109001 </C4>
</RECORD2>
<RECORD3>
<C2> 50000000022272</C2>
<C3>5718800000</C3>
<C4>200000103002</C4>
</RECORD3>
<RECORD1><!-- it should not come....This is the issue -->
<C2>5000000002227ABCD </C2> 
<C3>3014 </C3>
<C4> DELVRY02 </C4>
</RECORD1>
</shipment>
<shipment>
<RECORD1>
<C2>5000000002227ABCD </C2>
<C3>3014 </C3>
<C4> DELVRY02 </C4>
</RECORD1>
<RECORD2>
<C2> 50000000022272571880</C2>
<C3>000010000000200</C3>
<C4>9659666341109001 </C4>
</RECORD2>
<RECORD3>
<C2> 50000000022272</C2>
<C3>5718800000</C3>
<C4>200000103002</C4>
</RECORD3>
</shipment>
</Root-Element>

1 个答案:

答案 0 :(得分:0)

我相信这可以做你想要的:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
  <xsl:key name="items" match="Root-Element/*[name() != 'RECORD1']"
           use="preceding-sibling::RECORD1[1]"/>
  <xsl:template match="/Root-Element">
    <Root-Element>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="RECORD1"/>
    </Root-Element>
  </xsl:template>
  <xsl:template match="RECORD1">
    <shipment>
      <xsl:copy-of select="."/>
      <xsl:copy-of select="key('items', .)"/>
    </shipment>
  </xsl:template>
</xsl:stylesheet>

我用xsltproc和saxon测试过它。密钥不需要generate-id。您使用的代码的一个主要问题是密钥包含RECORD1在具有相同密钥的元素组中。由于它们是通过查看前面的RECORD1元素进行分组的,因此任何给定的RECORD1元素最终都会与其前面的组进行分组。解决方案是排除它。