在 xsl:for-each 中 xsl:value-of 的行为 [英] Behaviour of xsl:value-of inside an xsl:for-each

查看:33
本文介绍了在 xsl:for-each 中 xsl:value-of 的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当节点名称是同一文档中另一个节点的值时,我似乎在检索节点的值时遇到问题.让我举例说明.

I seem to have problem retrieving the value of a node when the node name is the value of another node in the same document. Let me illustrate.

首先是文档:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <FamilySmith>
        <Children>
            <Child>
                <Name>Thomas</Name>
                <School_Id>5489</School_Id>
            </Child>
            <Child>
                <Name>Andrew</Name>
                <School_Id>7766</School_Id>
            </Child>
        </Children>
    </FamilySmith>
</Doc>

我只想显示以下内容:托马斯去了英国伦敦高街的圣托马斯.安德鲁去爱丁堡干草市场的安德森男子学校

I simply want to display the following: Thomas goes to St Thomas at High Street, London, England. Andrew goes to Anderson Boys School at Haymarket, Edinborough

使用以下 xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
        <xsl:output method="text"/>

        <xsl:template match="/Doc">
        <xsl:apply-templates select="FamilySmith"/>
    </xsl:template>

    <xsl:template match="FamilySmith">
        <xsl:for-each select="Children/Child">
            <xsl:text>&#xa; </xsl:text>
            <xsl:value-of select="Name"/>
            <xsl:text> goes to </xsl:text>
            <xsl:value-of select="/Doc/Schools/School[Id = School_Id]/Name"/>
            <xsl:text> at </xsl:text>
            <xsl:value-of select="/Doc/Schools/School[Id = School_Id]/Address"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

但似乎表达式/Doc/Schools/School[Id = School_Id]... 不起作用,因为它每次都返回一个空值.结果如下.

But it seems that the expression /Doc/Schools/School[Id = School_Id]... does not work as it returns an empty value each time. The results are given below.

托马斯去了安德鲁前往

请问有什么优惠吗?谢谢.

Any offers pleaese? Thanks.

推荐答案

您正在寻找 current() 函数:

You are looking for the current() function:

<xsl:value-of select="/Doc/Schools/School[Id = current()/School_Id]/Name"/>

您之前的尝试是寻找具有相同值的 IdSchool_Id 孩子的 School 元素,而您实际想要的是比较 School 的 Id针对 ChildSchool_Id 子节点,该子节点是 for-each 的当前迭代的目标,即 当前上下文节点从谓词之外.这就是 current() 给你的.

Your previous attempt was looking for School elements that have an Id and a School_Id child that have the same value, whereas what you actually want is to compare the School's Id against the School_Id child of the Child that is the target of the current iteration of the for-each, i.e. the current context node from outside the predicate. This is what current() gives you.

这篇关于在 xsl:for-each 中 xsl:value-of 的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆