XSLT:如何从<node id =“$ variable”>获取值

时间:2018-04-03 07:04:29

标签: xml xslt

所以我想从一个id参数存储在$ variable中的节点获取一个子节点值。

XML代码模式:

<DocumentVersion id="not_relevant">
    <Specification id="id_2000_2">
        <Terminal id="not_relevant">
            <Name>Name1</Name>
            <Size>Size1</Size>
        </Terminal>
    </Specification>
    <Specification id="id_2000_0">
        <Node id="not_relevant">
            <Subnode id="not_relevant">
                <SubnodeNumber>1</SubnodeNumber>
                <SubnodeSpecification>id_2000_2</SubnodeSpecification>
            </Subnode>
            <Subnode id="not_relevant">
                <SubnodeNumber>2</SubnodeNumber>
                <SubnodeSpecification>id_2000_3</SubnodeSpecification>
            </Subnode>
            <Subnode id="not_relevant">
                <SubnodeNumber>1</SubnodeNumber>
                <SubnodeSpecification>id_2000_2</SubnodeSpecification>
            </Subnode>
        </Node>
    </Specification>
    <Specification id="id_2000_3">
        <Terminal id="not_relevant">
            <Name>Name2</Name>
            <Size>Size2</Size>
        </Terminal>
    </Specification>
</DocumentVersion>

XSLT代码(包括html表标签但你明白了):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.1">
    <xsl:output method="html" omit-xml-declaration="yes" indent="no"/>
    <xsl:template match="/">

        <xsl:for-each select="//Subnode[@id]">
            <tr>
                <td>
                    <xsl:value-of select="position()"/>
                </td>
                <td>
                    <xsl:value-of select="SubnodeNumber"/>
                </td>
                <xsl:variable name="specid" select="SubnodeSpecification"/>
                <td>
                    <xsl:value-of select="$specid"/>
                </td>
                <td>
                    <xsl:for-each select="//Specification[@id=$specid]">
                        <xsl:value-of select="//Size"></xsl:value-of>
                    </xsl:for-each>
                </td>
                <td>
                    <xsl:for-each select="//Specification[@id=$specid]">
                        <xsl:value-of select="//Name"></xsl:value-of>
                    </xsl:for-each>
                </td>
            </tr>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

仔细查看两个输出中的第2行。目前我总是得到Size1和Name1。 $ specid包含正确的id,但不知何故,XSLT只能看到id_2000_2的值。

当前输出:

<SubnodeNumber>      $specid             <Size>            <Name>
    1                id_2000_2            Size1            Name1
    2                id_2000_3            Size1            Name1
    3                id_2000_2            Size1            Name1

期望的输出:

<SubnodeNumber>      $specid             <Size>            <Name>
    1                id_2000_2            Size1            Name1
    2                id_2000_3            Size2            Name2
    3                id_2000_2            Size1            Name1

1 个答案:

答案 0 :(得分:0)

您的示例数据格式不正确,错过了两个结束标记。

此外,它似乎不一致,您有两次id_2000_2,但数据中没有id_2000_3。

假设这些问题已修复,您可以通过替换

来修复样式表
 <xsl:value-of select="//Name"></xsl:value-of>

 <xsl:value-of select=".//Name"></xsl:value-of>

 <xsl:value-of select="Terminal/Name"></xsl:value-of>

(从for-each中的当前上下文节点开始选择。)

以便从当前内容中选择Name元素。

完整XML:

<DocumentVersion id="not_relevant">
<Specification id="id_2000_2">
    <Terminal id="not_relevant">
        <Name>Name1</Name>
        <Size>Size1</Size>
    </Terminal>
</Specification>
<Specification id="id_2000_0">
    <Node id="not_relevant">
        <Subnode id="not_relevant">
            <SubnodeNumber>1</SubnodeNumber>
            <SubnodeSpecification>id_2000_2</SubnodeSpecification>
        </Subnode>
        <Subnode id="not_relevant">
            <SubnodeNumber>2</SubnodeNumber>
            <SubnodeSpecification>id_2000_3</SubnodeSpecification>
        </Subnode>
        <Subnode id="not_relevant">
            <SubnodeNumber>1</SubnodeNumber>
            <SubnodeSpecification>id_2000_2</SubnodeSpecification>
        </Subnode>
    </Node> <!-- Changed -->
</Specification>
<Specification id="id_2000_3"> <!-- Changed -->
    <Terminal id="not_relevant">
        <Name>Name2</Name>
        <Size>Size2</Size>
    </Terminal>
</Specification>

完整XSLT:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1">
    <xsl:output method="html" omit-xml-declaration="yes" indent="no"/>
    <xsl:template match="/">
        <xsl:for-each select="//Subnode[@id]">
            <tr>
                <td>
                    <xsl:value-of select="position()"/>
                </td>
                <td>
                    <xsl:value-of select="SubnodeNumber"/>
                </td>
                <xsl:variable name="specid" select="SubnodeSpecification"/>
                <td>
                    <xsl:value-of select="$specid"/>
                </td>
                <td>
                    <xsl:for-each select="//Specification[@id=$specid]">
                        <xsl:value-of select=".//Size"></xsl:value-of>
                    </xsl:for-each>
                </td>
                <td>
                    <xsl:for-each select="//Specification[@id=$specid]">
                        <xsl:value-of select=".//Name"></xsl:value-of>
                    </xsl:for-each>
                </td>
            </tr>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

输出:

<tr>
    <td>1</td>
    <td>1</td>
    <td>id_2000_2</td>
    <td>Size1</td>
    <td>Name1</td>
 </tr>
 <tr>
    <td>2</td>
    <td>2</td>
    <td>id_2000_3</td>
    <td>Size2</td>
    <td>Name2</td>
 </tr>
 <tr>
    <td>3</td>
    <td>1</td>
    <td>id_2000_2</td>
    <td>Size1</td>
    <td>Name1</td>
 </tr>