2013-03-26 73 views
0
<Sections> 
    <Products> 
     <Transport> 
     <TransportSequence>1</TransportSequence> 
     <Traveller>001</Traveller> 
     </Transport> 
     <Transport> 
     <TransportSequence>2</TransportSequence> 
     <Traveller>001</Traveller> 
     </Transport> 
    </Products> 
    </Sections> 
    <Sections> 
    <Products> 
     <Transport> 
     <TransportSequence>1</TransportSequence> 
     <Traveller>002</Traveller> 
     </Transport> 
     <Transport> 
     <TransportSequence>2</TransportSequence> 
     <Traveller>002</Traveller> 
     </Transport> 
    </Products> 
    </Sections> 

我對某些XML的排序有特定的問題。從上面的例子中我需要改變格式,以便我選擇不同的TransportSequence。那麼我就需要指定任何「旅行者」節點作爲孩子產生這樣的:XSLT轉換:選擇distinct和appendchild組

<Sections> 
    <Products> 
     <Transport> 
     <TransportSequence>1</TransportSequence> 
     <Travellers> 
      <Traveller>001</Traveller> 
      <Traveller>002</Traveller> 
     </Travellers> 
     </Transport> 
     <Transport> 
     <TransportSequence>2</TransportSequence> 
     <Travellers> 
      <Traveller>001</Traveller> 
      <Traveller>002</Traveller> 
     </Travellers> 
     </Transport> 
    </Products> 
</Sections> 

另一個問題是,在交通運輸節點也包含了不少的孩子和孫子節點在此示例中未顯示。也可以有許多Trailer屬於TravellerSequence。還有很多TransportSequence號碼。

+0

是否要使用XSLT 1.0或2.0?在XSLT 2.0中使用'for-each-group'分組更容易。 – 2013-03-26 13:58:38

回答

0

下面是一個XSLT 2.0樣式表來使用XSLT運行2.0處理器等撒克遜9或AltovaXML:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0"> 

<xsl:output indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="*[Sections]"> 
    <xsl:copy> 
    <Sections> 
     <Products> 
     <xsl:for-each-group select="Sections/Products/Transport" group-by="TransportSequence"> 
      <Transport> 
      <TransportSequence><xsl:value-of select="current-grouping-key()"/></TransportSequence> 
      <Travellers> 
       <xsl:copy-of select="current-group()/Traveller"/> 
      </Travellers> 
      </Transport> 
     </xsl:for-each-group> 
     </Products> 
    </Sections> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

它把

<Root> 
<Sections> 
    <Products> 
     <Transport> 
     <TransportSequence>1</TransportSequence> 
     <Traveller>001</Traveller> 
     </Transport> 
     <Transport> 
     <TransportSequence>2</TransportSequence> 
     <Traveller>001</Traveller> 
     </Transport> 
    </Products> 
    </Sections> 
    <Sections> 
    <Products> 
     <Transport> 
     <TransportSequence>1</TransportSequence> 
     <Traveller>002</Traveller> 
     </Transport> 
     <Transport> 
     <TransportSequence>2</TransportSequence> 
     <Traveller>002</Traveller> 
     </Transport> 
    </Products> 
    </Sections> 
</Root> 

<Root> 
    <Sections> 
     <Products> 
     <Transport> 
      <TransportSequence>1</TransportSequence> 
      <Travellers> 
       <Traveller>001</Traveller> 
       <Traveller>002</Traveller> 
      </Travellers> 
     </Transport> 
     <Transport> 
      <TransportSequence>2</TransportSequence> 
      <Travellers> 
       <Traveller>001</Traveller> 
       <Traveller>002</Traveller> 
      </Travellers> 
     </Transport> 
     </Products> 
    </Sections> 
</Root> 

[編輯]要完成答案,如果您想使用XSLT 1.0處理器,使用Muenchian分組的解決方案看起來就像一個s如下:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

<xsl:output indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:key name="by-seq" match="Sections/Products/Transport" use="TransportSequence"/> 

<xsl:template match="*[Sections]"> 
    <xsl:copy> 
    <Sections> 
     <Products> 
     <xsl:for-each select="Sections/Products/Transport[generate-id() = generate-id(key('by-seq', TransportSequence)[1])]"> 
      <Transport> 
      <xsl:copy-of select="TransportSequence"/> 
      <Travellers> 
       <xsl:copy-of select="key('by-seq', TransportSequence)/Traveller"/> 
      </Travellers> 
      </Transport> 
     </xsl:for-each> 
     </Products> 
    </Sections> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

非常感謝,這是我指出了正確的方向,並解決了很多問題。 – BerZerK 2013-03-26 16:13:22