<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上选择distinct。然后,我需要将任何“Traveler”节点指定为子节点,以生成如下内容:
<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>
另一个问题是,在Transport节点中还包含许多未在此示例中显示的子节点和孙节点。还有许多属于TravellerSequence的travllers。还有许多TransportSequence数字。
答案 0 :(得分:0)
这是一个XSLT 2.0样式表,可以使用Saxon 9或AltovaXML等XSLT 2.0处理器运行:
<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>
[edit]要完成答案,如果您想使用XSLT 1.0处理器,使用Muenchian分组的解决方案如下所示:
<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>