XSLT—对XML元素的子集进行排序,同时将未排序的元素保留在其位置
我试图提出一种XSLT,它将对下面XML中的元素进行排序,同时保留其他元素的位置XSLT—对XML元素的子集进行排序,同时将未排序的元素保留在其位置,xslt,Xslt,我试图提出一种XSLT,它将对下面XML中的元素进行排序,同时保留其他元素的位置 <TransactionSet TransactionSet_Id="12345"> <ATransaction ATransaction_Id="54321"> <DoNotSort_1 DoNotSort_1_Id="90678"> <SomeData/> </DoNotSort_1> <DoNotSort
<TransactionSet TransactionSet_Id="12345">
<ATransaction ATransaction_Id="54321">
<DoNotSort_1 DoNotSort_1_Id="90678">
<SomeData/>
</DoNotSort_1>
<DoNotSort_2 DoNotSort_2_Id="46456">
<OtherData/>
</DoNotSort_2>
<DoNotSort_3 DoNotSort_3_Id="33333"/>
<SortIt SortIt_Id="11">
<TheOrder>1</TheOrder>
<MoreData/>
</SortIt>
<SortIt SortIt_Id="55">
<TheOrder>5</TheOrder>
<MoreData/>
</SortIt>
<SortIt SortIt_Id="22">
<TheOrder>2</TheOrder>
<MoreData/>
</SortIt>
<SortIt SortIt_Id="44">
<TheOrder>4</TheOrder>
<MoreData/>
</SortIt>
<DoNotSort_4 DoNotSort_4_Id="789456">
<EvenMoreData/>
</DoNotSort_4>
<DoNotSort_4 DoNotSort_4_Id="899567">
<EvenMoreData/>
</DoNotSort_4>
<DoNotSort_5 DoNotSort_5_Id="55555">
<EvenMoreData/>
</DoNotSort_5>
</ATransaction>
我应用的XSLT如下所示:
<xsl:stylesheet
version="1.0"
xmlns="http://www.oneshield.com/DragonSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="ATransaction">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates>
<xsl:sort select= "TheOrder" data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<TransactionSet TransactionSet_Id="12345">
<ATransaction ATransaction_Id="54321">
<DoNotSort_1 DoNotSort_1_Id="90678">
<SomeData/>
</DoNotSort_1>
<DoNotSort_2 DoNotSort_2_Id="46456">
<OtherData/>
</DoNotSort_2>
<DoNotSort_3 DoNotSort_3_Id="33333"/>
<DoNotSort_4 DoNotSort_4_Id="789456">
<EvenMoreData/>
</DoNotSort_4>
<DoNotSort_4 DoNotSort_4_Id="899567">
<EvenMoreData/>
</DoNotSort_4>
<DoNotSort_5 DoNotSort_5_Id="55555">
<EvenMoreData/>
</DoNotSort_5>
<SortIt SortIt_Id="11">
<TheOrder>1</TheOrder>
<MoreData/>
</SortIt>
<SortIt SortIt_Id="22">
<TheOrder>2</TheOrder>
<MoreData/>
</SortIt>
<SortIt SortIt_Id="44">
<TheOrder>4</TheOrder>
<MoreData/>
</SortIt>
<SortIt SortIt_Id="55">
<TheOrder>5</TheOrder>
<MoreData/>
</SortIt>
</ATransaction>
</TransactionSet>
我得到的输出XML如下所示:
<xsl:stylesheet
version="1.0"
xmlns="http://www.oneshield.com/DragonSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="ATransaction">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates>
<xsl:sort select= "TheOrder" data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<TransactionSet TransactionSet_Id="12345">
<ATransaction ATransaction_Id="54321">
<DoNotSort_1 DoNotSort_1_Id="90678">
<SomeData/>
</DoNotSort_1>
<DoNotSort_2 DoNotSort_2_Id="46456">
<OtherData/>
</DoNotSort_2>
<DoNotSort_3 DoNotSort_3_Id="33333"/>
<DoNotSort_4 DoNotSort_4_Id="789456">
<EvenMoreData/>
</DoNotSort_4>
<DoNotSort_4 DoNotSort_4_Id="899567">
<EvenMoreData/>
</DoNotSort_4>
<DoNotSort_5 DoNotSort_5_Id="55555">
<EvenMoreData/>
</DoNotSort_5>
<SortIt SortIt_Id="11">
<TheOrder>1</TheOrder>
<MoreData/>
</SortIt>
<SortIt SortIt_Id="22">
<TheOrder>2</TheOrder>
<MoreData/>
</SortIt>
<SortIt SortIt_Id="44">
<TheOrder>4</TheOrder>
<MoreData/>
</SortIt>
<SortIt SortIt_Id="55">
<TheOrder>5</TheOrder>
<MoreData/>
</SortIt>
</ATransaction>
</TransactionSet>
我的问题是:即使对元素进行了排序,和元素也被移动到了元素之上。我希望他们在恶劣的环境下处于适当的位置
在此方面的任何帮助都将不胜感激。提前感谢。在XSLT 2或3中,假设您希望对相邻的SortIt元素进行排序,您可以对每个组使用nextended=booleanself::SortIt。在XSLT 1中,您可以尝试使用一个键来标识相邻的SortIt,然后对它们进行处理和排序:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:key name="adjacent" match="SortIt[preceding-sibling::*[1][self::SortIt]]" use="generate-id(preceding-sibling::SortIt[not(preceding-sibling::*[1][self::SortIt])][1])"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="SortIt[preceding-sibling::*[1][self::SortIt]]"/>
<xsl:template match="SortIt[not(preceding-sibling::*[1][self::SortIt])]">
<xsl:for-each select=". | key('adjacent', generate-id())">
<xsl:sort select="TheOrder" data-type="number"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
如果要排序的节点始终位于单个连续块中,则只需执行以下操作: XSLT1.0
如果SortIt元素和DoNotSort元素穿插在一起,您希望得到什么输出?对于XSLT问题,请始终告诉我们XSLT版本。大多数问题在2.0或3.0中更容易解决,但许多用户都停留在1.0上,因此这会带来真正的不同。