我很惭愧地承认,我已连续7个小时试图使这一工作。我希望使用第二个节点的顺序对第一个节点的输出进行排序。
XML
<?xml version="1.0" encoding="utf-8" ?>
<data>
<content>
<section link-id="86" link-handle="Start" value="Start" />
<section link-id="23" link-handle="george-orwell" value="Orwell, George" />
<section link-id="24" link-handle="aldous-huxley" value="Huxley, Aldous" />
<section link-id="26" link-handle="robert-lewis-stevenson" value="Stevenson, Robert Louis" />
</content>
<datasource>
<entry id="86">
<name handle="start">Start</name>
<order handle="0">0</order>
</entry>
<entry id="23">
<name handle="george-orwell">Orwell, George</name>
<order handle="1">1</order>
</entry>
<entry id="26">
<name handle="robert-lewis-stevenson">Stevenson, Robert Louis</name>
<order handle="2">2</order>
</entry>
<entry id="24">
<name handle="aldous-huxley">Huxley, Aldous</name>
<order handle="3">3</order>
</entry>
</datasource>
</data>XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="sort-key" match="/data/datasource/entry" use="." />
<xsl:template match="data">
<html>
<body>
<xsl:apply-templates select="content" />
</body>
</html>
</xsl:template>
<xsl:template match="content">
<xsl:apply-templates select="section">
<xsl:sort select="key('sort-key', .)/@id"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="section">
Section: <xsl:value-of select="@value"/>
</xsl:template>
</xsl:stylesheet>电流和不正确的输出
<?xml version="1.0" encoding="utf-16"?>
<html><body>
Section: Start
Section: Orwell, George
Section: Huxley, Aldous
Section: Stevenson, Robert Louis</body>
</html>发布于 2013-11-25 08:39:58
从您的XML中可以看出,您实际上希望通过它们的id来查找条目元素,因此您可能应该像这样定义您的键:
<xsl:key name="sort-key" match="/data/datasource/entry" use="@id" />然后,假设句柄属性始终按顺序顺序排列,则它们的xsl:命令如下所示
<xsl:sort select="key('sort-key', @link-id)/order/@handle"/>试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="sort-key" match="/data/datasource/entry" use="@id" />
<xsl:template match="data">
<html>
<body>
<xsl:apply-templates select="content" />
</body>
</html>
</xsl:template>
<xsl:template match="content">
<xsl:apply-templates select="section">
<xsl:sort select="key('sort-key', @link-id)/order/@handle"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="section">
Section: <xsl:value-of select="@value"/>
</xsl:template>
</xsl:stylesheet>但你是从错误的方向来的吗?为什么不从直接选择第二个节点开始,然后使用xsl:apply-templates使用键选择相应的第一个节点。
尝试使用此XSLT作为替代方法。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="section" match="section" use="@link-id" />
<xsl:template match="data">
<html>
<body>
<xsl:apply-templates select="datasource/entry" />
</body>
</html>
</xsl:template>
<xsl:template match="entry">
<xsl:apply-templates select="key('section', @id)" />
</xsl:template>
<xsl:template match="section">
Section: <xsl:value-of select="@value"/>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/20183967
复制相似问题