XSLT设置值取决于不同元素的比较

时间:2013-05-22 01:21:18

标签: xslt

好的,这很难解释,但我需要的是根据来自一系列节点的当前元素是否与来自另一系列节点的任何其他元素匹配来设置值。这可能是更好的说明,所以这里有一个粗略的例子,我在问。 假设我有一个带有以下

的xml
<Person>
   <name>John Smith</name>
   <id>25</age>
   ....
</Person>
<Person>
    <name>Smith, Will</name>
    <id>22</age>
   ....
</Person>
and so on and so on

<name>
    <given>Smith, Will</given>
    <alternate>Will Smith</alternate>
</name>
<name>
    <given>BobbyJ.</given>
    <alternate>Bobby Johnson</alternate>
</name>
and so on and so on.

我需要的是,如果姓名Smith,Will出现在名称中的任何给定元素中,则将值设置为替代元素,否则将其作为人物的名称。例如,我的例子将打印出来

John Smith

Will Smith

作为结果。

希望这可以在XSLT中完成,最好是版本1.0,因为它是一种防止错误写入值的安全措施,以确保它们的格式正确,请注意这不是我正在使用的示例,因为我是我继承的混乱代码要复杂得多,这应该是一个相关的例子,应该基本上展示我需要的东西,并希望能够让其他人明白。我正在使用的XSL示例如果它有帮助

<xsl:template match="Person">
    <xsl:variable name="person" select="."/>
    <div class="...">
        <div class="...">
            <xsl:value-of select="id"/>
            <xsl:for-each select="//name">
                    <xsl:choose>
                            <xsl:when test="given = $person/name">
                                <xsl:value-of select="alternate"/>
                            </xsl:when>
                        </xsl:choose>
                </xsl:for-each>

如果有必要,我会提供alternate名称但不会given名称。如果不在name元素中,我需要提供name的值。

1 个答案:

答案 0 :(得分:0)

<xsl:template match="Person">
    <div class="...">
        <div class="...">
            <xsl:value-of select="id"/>
            <xsl:choose>
              <xsl:when test="//name[given = current()/name]/alternate">
                <xsl:value-of select="//name[given = current()/name]/alternate"/>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:value-of select="name"/>
              </xsl:otherwise>
            </xsl:choose>
        </div>
    </div>
</xsl:template>
相关问题