【发布时间】:2021-03-14 07:39:43
【问题描述】:
我正在尝试转换一个输入 XML,其中所有父节点都说“x”需要被提取、排序,然后根据它们的子节点说“y”值在最终输出 XML 中重新排序。下面是我的 XML,我正在尝试对其进行修改,以便最终的 XML 将元素按升序排列,其中排序是基于 xpath notes/notesBODY/group/text 中的数值完成的,基本上是元素 <text>1. </text> 存在于所有 @987654323 @元素。但是输出 XML 并不像预期的那样,因为我似乎无法弄清楚如何在 XSLT 中准确地使用排序。任何帮助表示赞赏。
输入 XML:
<root>
<group>
<text>group1</text>
</group>
<group>
<text>group2</text>
</group>
<notes>
<notesText>notes1</notesText>
<notesBODY>
<group>
<text>2. </text>
<text>notesbody1</text>
</group>
</notesBODY>
</notes>
<notes>
<notesText>notes2</notesText>
<notesBODY>
<group>
<text>1. </text>
<text>notesbody2</text>
</group>
</notesBODY>
</notes>
<notes>
<notesText>notes3</notesText>
<notesBODY>
<group>
<text>3. </text>
<text>notesbody3</text>
</group>
</notesBODY>
</notes>
<group>
<text>group3</text>
<notes>
<notesText>notes4</notesText>
<notesBODY>
<group>
<text>4. </text>
<text>notesbody4</text>
</group>
</notesBODY>
</notes>
</group>
<group>
<text>group4</text>
</group>
<group>
<text>group5</text>
<notes>
<notesText>notes5</notesText>
<notesBODY>
<group>
<text>6. </text>
<text>notesbody5</text>
</group>
</notesBODY>
</notes>
</group>
<notes>
<notesText>notes6</notesText>
<notesBODY>
<group>
<text>5. </text>
<text>notesbody6</text>
</group>
</notesBODY>
</notes>
<group>
<text>group6</text>
</group>
<group>
<text>group7</text>
</group>
<group>
<text>group8</text>
<notes>
<notesText>notes7</notesText>
<notesBODY>
<group>
<text>8. </text>
<text>notesbody7</text>
</group>
</notesBODY>
</notes>
</group>
<notes>
<notesText>notes8</notesText>
<notesBODY>
<group>
<text>7. </text>
<text>notesbody8</text>
</group>
</notesBODY>
</notes>
<group>
<text>group9</text>
</group>
</root>
预期输出:
<root>
<group>
<text>group1</text>
</group>
<group>
<text>group2</text>
</group>
<group>
<text>group3</text>
</group>
<group>
<text>group4</text>
</group>
<group>
<text>group5</text>
</group>
<group>
<text>group6</text>
</group>
<group>
<text>group7</text>
</group>
<group>
<text>group8</text>
</group>
<group>
<text>group9</text>
</group>
<notes>
<notesText>notes2</notesText>
<notesBODY>
<group>
<text>1. </text>
<text>notesbody2</text>
</group>
</notesBODY>
</notes>
<notes>
<notesText>notes1</notesText>
<notesBODY>
<group>
<text>2. </text>
<text>notesbody1</text>
</group>
</notesBODY>
</notes>
<notes>
<notesText>notes3</notesText>
<notesBODY>
<group>
<text>3. </text>
<text>notesbody3</text>
</group>
</notesBODY>
</notes>
<notes>
<notesText>notes4</notesText>
<notesBODY>
<group>
<text>4. </text>
<text>notesbody4</text>
</group>
</notesBODY>
</notes>
<notes>
<notesText>notes6</notesText>
<notesBODY>
<group>
<text>5. </text>
<text>notesbody6</text>
</group>
</notesBODY>
</notes>
<notes>
<notesText>notes5</notesText>
<notesBODY>
<group>
<text>6. </text>
<text>notesbody5</text>
</group>
</notesBODY>
</notes>
<notes>
<notesText>notes8</notesText>
<notesBODY>
<group>
<text>7. </text>
<text>notesbody8</text>
</group>
</notesBODY>
</notes>
<notes>
<notesText>notes7</notesText>
<notesBODY>
<group>
<text>8. </text>
<text>notesbody7</text>
</group>
</notesBODY>
</notes>
</root>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="notes">
<xsl:apply-templates select="@*" />
<xsl:copy>
<xsl:apply-templates select="node()">
<xsl:sort select="notesBODY/group/text[position() = 0]" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
【问题讨论】: