首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据另一个XML节点的顺序对其进行排序?

如何根据另一个XML节点的顺序对其进行排序?
EN

Stack Overflow用户
提问于 2013-11-25 02:56:01
回答 1查看 412关注 0票数 0

我很惭愧地承认,我已连续7个小时试图使这一工作。我希望使用第二个节点的顺序对第一个节点的输出进行排序。

XML

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>

电流和不正确的输出

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-16"?>
<html><body>
Section: Start
Section: Orwell, George
Section: Huxley, Aldous
Section: Stevenson, Robert Louis</body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2013-11-25 08:39:58

从您的XML中可以看出,您实际上希望通过它们的id来查找条目元素,因此您可能应该像这样定义您的键:

代码语言:javascript
复制
<xsl:key name="sort-key" match="/data/datasource/entry" use="@id" />

然后,假设句柄属性始终按顺序顺序排列,则它们的xsl:命令如下所示

代码语言:javascript
复制
<xsl:sort select="key('sort-key', @link-id)/order/@handle"/>

试试这个XSLT

代码语言:javascript
复制
<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作为替代方法。

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20183967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档