[英]XML to XML with XSLT Sorting
我有幾個巨大的XML,我只需要對其中的一小部分進行排序。 作為輸出,我應該具有相同的XML,但具有排序的子結構。 這是一個例子:
<testStructure>
<parentStruct>
<firstpPreChild>some value here</firstpPreChild>
<secondPreChild>some other value</secondPreChild>
<thirdPreChild>third value here</thirdPreChild>
<fourtPreChild>fourth value here</fourtPreChild>
<struct id="5">
<num>5</num>
</struct>
<struct id="4">
<num>4</num>
</struct>
<struct id="1">
<num>1</num>
</struct>
<struct id="2">
<num>2</num>
</struct>
<struct id="3">
<num>3</num>
</struct>
<firstAdditionalChild>some value here</firstAdditionalChild>
<secondAdditionalChild>some other value</secondAdditionalChild>
<thirdAdditionalChild>third value here</thirdAdditionalChild>
<fourtAdditionalChild>fourth value here</fourtAdditionalChild>-->
</parentStruct>
<otherStruct>
<firstChild>some value here</firstChild>
<secondChild>some other value</secondChild>
<thirdChild>third value here</thirdChild>
<fourtChild>fourth value here</fourtChild>
</otherStruct>
應該轉化為
<testStructure>
<parentStruct>
<firstpPreChild>some value here</firstpPreChild>
<secondPreChild>some other value</secondPreChild>
<thirdPreChild>third value here</thirdPreChild>
<fourtPreChild>fourth value here</fourtPreChild>
<struct id="1">
<num>1</num>
</struct>
<struct id="2">
<num>2</num>
</struct>
<struct id="3">
<num>3</num>
</struct>
<struct id="4">
<num>4</num>
</struct>
<struct id="5">
<num>5</num>
</struct>
<firstAdditionalChild>some value here</firstAdditionalChild>
<secondAdditionalChild>some other value</secondAdditionalChild>
<thirdAdditionalChild>third value here</thirdAdditionalChild>
<fourtAdditionalChild>fourth value here</fourtAdditionalChild>-->
</parentStruct>
<otherStruct>
<firstChild>some value here</firstChild>
<secondChild>some other value</secondChild>
<thirdChild>third value here</thirdChild>
<fourtChild>fourth value here</fourtChild>
</otherStruct>
作為排序標准,可以使用num
或@id
。 我試過這樣的變體:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
可以,但是將排序后的結構從其原始位置移開。 不幸的是,我需要與輸出相同的結構。
先謝謝您的幫助!
XSLT 1.0樣式表按相鄰分組然后排序:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kStructByFirstPreceding"
match="struct"
use="generate-id(
preceding-sibling::struct[
not(preceding-sibling::*[1]/self::struct)
][1]
)"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="struct[not(preceding-sibling::*[1]/self::struct)]">
<xsl:apply-templates select=".|key('kStructByFirstPreceding',
generate-id())"
mode="copy">
<xsl:sort select="@id"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="struct"/>
<xsl:template match="node()" mode="copy">
<xsl:call-template name="identity"/>
</xsl:template>
</xsl:stylesheet>
輸出:
<testStructure>
<parentStruct>
<firstpPreChild>some value here</firstpPreChild>
<secondPreChild>some other value</secondPreChild>
<thirdPreChild>third value here</thirdPreChild>
<fourtPreChild>fourth value here</fourtPreChild>
<struct id="1">
<num>1</num>
</struct>
<struct id="2">
<num>2</num>
</struct>
<struct id="3">
<num>3</num>
</struct>
<struct id="4">
<num>4</num>
</struct>
<struct id="5">
<num>5</num>
</struct>
<firstAdditionalChild>some value here</firstAdditionalChild>
<secondAdditionalChild>some other value</secondAdditionalChild>
<thirdAdditionalChild>third value here</thirdAdditionalChild>
<fourtAdditionalChild>fourth value here</fourtAdditionalChild>-->
</parentStruct>
<otherStruct>
<firstChild>some value here</firstChild>
<secondChild>some other value</secondChild>
<thirdChild>third value here</thirdChild>
<fourtChild>fourth value here</fourtChild>
</otherStruct>
</testStructure>
更簡單的XSLT 2.0解決方案:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[struct]">
<xsl:copy>
<xsl:for-each-group select="*"
group-adjacent="boolean(self::struct)">
<xsl:apply-templates select="current-group()">
<xsl:sort select="@id[current-grouping-key()]"/>
</xsl:apply-templates>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.