xslt-1.0 - 使用 xsl :value-of in for-each loop over a xsl:variable

标签 xslt-1.0 xsl-variable

这是条目 25317199 中问题的续集.

在 25317199 帖子中,数据有 2 个 block ,即 SchoolsFamilySmithFamilySmith 中的数据用作检索Schools 中数据的 key 。

现在,在这种情况下,数据被拆分,FamilySmith 现在被定义为样式表中的一个变量,如下图所示:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    extension-element-prefixes="exsl"
    version="1.0">

    <xsl:variable name="FamilySmith">
        <Children>
            <Child>
                <Name>Thomas</Name>
                <School_Id>5489</School_Id>
            </Child>
            <Child>
                <Name>Andrew</Name>
                <School_Id>7766</School_Id>
            </Child>
        </Children>
    </xsl:variable>

    <xsl:template match="/Doc">
        <xsl:for-each select="exsl:node-set($FamilySmith)/Children/Child">
            <xsl:text>&#xa;</xsl:text>
            <xsl:value-of select="Name"/>
            <xsl:text> goes to (school's name here) </xsl:text>
            <xsl:value-of select="/Doc/Schools/School[Id = current()/School_Id]/Name"/>
            <xsl:text> at (school's address here) </xsl:text>
            <xsl:value-of select="/Doc/Schools/School[Id = current()/School_Id]/Address"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

这适用于以下 XML 数据:

<Doc>
    <Schools>
        <School>
            <Id>5489</Id>
            <Name>St Thomas</Name>
            <Address>High Street, London, England</Address>
        </School>
        <School>
            <Id>7766</Id>
            <Name>Anderson Boys School</Name>
            <Address>Haymarket, Edinborough</Address>
        </School>
    </Schools>
</Doc>

学校名称和学校地址的检索都产生空字符串,如下所示。

Thomas goes to (school's name here)  at (school's address here) 
Andrew goes to (school's name here)  at (school's address here) 

我已经使用了上一篇文章中给出的建议 25317199 ,即,使用 current() 来识别“来自谓词外部的当前上下文节点”。但似乎问题出在别处。请指教。非常感谢。

最佳答案

问题在于,以 / 开头的绝对路径解析为相对于包含当前上下文节点的树 的根节点,这不一定是输入 XML 文档。里面

<xsl:for-each select="exsl:node-set($FamilySmith)/Children/Child">

路径 /Doc/Schools 正在从 $FamilySmith 派生的临时文档中寻找 Doc 元素(所以 value-of 指令不选择任何内容)。您需要将 for-each 外部的上下文节点保存在另一个变量中:

<xsl:template match="/Doc">
    <xsl:variable name="doc" select="." />
    <xsl:for-each select="exsl:node-set($FamilySmith)/Children/Child">
        <xsl:text>&#xa;</xsl:text>
        <xsl:value-of select="Name"/>
        <xsl:text> goes to (school's name here) </xsl:text>
        <xsl:value-of select="$doc/Schools/School[Id = current()/School_Id]/Name"/>
        <xsl:text> at (school's address here) </xsl:text>
        <xsl:value-of select="$doc/Schools/School[Id = current()/School_Id]/Address"/>
    </xsl:for-each>
</xsl:template>

关于xslt-1.0 - 使用 xsl :value-of in for-each loop over a xsl:variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25350701/

相关文章:

java - 如何使用 XSLT 从 XML 生成完整列表和选定的详细信息?

xml - 如何在xslt中添加节点?

xml - 使用 XSLT 从 SSRS/Visual Studio 导出到 XML

xml - 在 XSLT 中查找两个日期时间之间的差异

java - xsl变量初始化,方法不被调用

xml - 查找xml文档中价数最高的元素

xslt - 是 xsl :sequence always non-empty?

xml - 使用翻译功能删除 XSLT 中的单词 'and'