【问题标题】:XSL group by node valueXSL 按节点值分组
【发布时间】:2017-03-13 14:17:01
【问题描述】:

我有一个 XSL 键分组的案例。目标是根据 ID 匹配替换值。

输入:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<message-in>
    <actions>
        <stop>
            <action id="33632">
                <text>DefaultComment</text>
                <planning-status>finished</planning-status>
                <realised-times>
                    <starttime>2017-03-13T12:43:54</starttime>
                    <finishtime>2017-03-13T13:15:21</finishtime>
                </realised-times>
            </action>
        </stop>
        <stop>
            <action id="33635">
                <planning-status>started</planning-status>
                <realised-times>
                    <starttime>2017-03-13T13:15:21</starttime>
                </realised-times>
            </action>
        </stop>
    </actions>
</message-in>
<output_getquerydata>
    <queries>
        <query name="fat">
            <parameters>
                <parameter name="id">33632</parameter>
            </parameters>
            <queryErrors/>
            <queryResults>
                <record id="1">
                    <column name="interfaceAction_externalId">33633OREA</column>
                </record>
            </queryResults>
        </query>
    </queries>
</output_getquerydata>
<output_getquerydata>
    <queries>
        <query name="fat">
            <parameters>
                <parameter name="id">33635</parameter>
            </parameters>
            <queryErrors/>
            <queryResults>
                <record id="1">
                    <column name="interfaceAction_externalId">536313OREA</column>
                </record>
            </queryResults>
        </query>
    </queries>
</output_getquerydata>
</root>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="utf-8"/>
<xsl:strip-space elements="*"/>
<xsl:key name="actionKey" match="stop" use="action/@id"/>
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="/">
    <root>
        <xsl:copy-of select="//message-in"/>
    </root>
</xsl:template>
<xsl:template match="action/@id">
    <xsl:attribute name="id"><xsl:value-of select="substring-before(//output_getquerydata/queries/query/parameters/parameter[key('actionKey', @id)]/queryResults/record/column[@name='interfaceAction_externalId'],'OREA')"/></xsl:attribute>
</xsl:template>
</xsl:stylesheet>

我有多个具有匹配“查询”节点的“操作”节点。 目标是,对于每个动作 ID,我们需要将“动作”标签中的 ID 值替换为相应的“interfaceAction_externalId”值。 因此,对于动作 ID 33632,我们将复制并替换为“33633”的值(因为我们在 parameneter/name/@id 33632 = action/@id 中有匹配项)。

副本效果很好,我得到了我需要的所有信息,但似乎 action/@id 没有被替换。 我以为我会使用一个键来保存 action/@id 的值,然后在模板匹配中使用它来替换 interfaceAction_externalId 中的值,但是它不起作用,我不确定我在做什么错了。

感谢您的帮助!

【问题讨论】:

    标签: xslt xslt-1.0 xslkey


    【解决方案1】:

    整个方法只有在您使用&lt;xsl:apply-templates select="//message-in"/&gt; 而不是&lt;xsl:copy-of select="//message-in"/&gt; 时才有意义。

    至于使用钥匙,我想你想要

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="utf-8"/>
        <xsl:strip-space elements="*"/>
        <xsl:key name="ref-query"
            match="output_getquerydata/queries/query/queryResults/record/column[@name='interfaceAction_externalId']"
            use="ancestor::query/parameters/parameter[@name = 'id']"/>
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="/">
            <root>
                <xsl:apply-templates select="//message-in"/>
            </root>
        </xsl:template>
        <xsl:template match="action/@id">
            <xsl:attribute name="id"><xsl:value-of select="substring-before(key('ref-query', .),'OREA')"/></xsl:attribute>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 嗯,好吧,因为你想重写以使用应用模板,现在输出是 id='' 所以我想可能是键匹配不正确或者我使用了参数[ key()] 错了吗?
    • 查看编辑,我认为您需要一个不同的键来引用动作 ID 中的参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2021-01-23
    • 2021-03-14
    • 2023-01-24
    • 2010-12-17
    相关资源
    最近更新 更多