XSLT访问for-each之外的不同标记

时间:2017-09-22 10:00:43

标签: xslt

我想通过for循环中的条件输出不同的标记(活动)值。但是不能通过这种方法来做任何建议吗?

数据:

    <ss>
    <identifier>
        <system>
            <value value="10" />
        </system>
        <system>
            <value value="15"/>
        </system>
        <system>
            <value value="789"/>
        </system>
    </identifier>
    <active value="123" />
</ss>

Xslt:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL /Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="ss">
        <Patient
            xmlns="http://hl7.org/fhir">
            <identifier>
                <xsl:for-each select="identifier/system/value">
                    <xsl:variable name="aa">
                        <xsl:if test="@value='10'">
                            <xsl:value-of select="active/@value" />
                        </xsl:if>
                    </xsl:variable>
                    <gender value="{$aa}"/>
                </xsl:for-each>
            </identifier>
        </Patient>
    </xsl:template>
</xsl:stylesheet>

输出: 我期望从上面的代码输出

<?xml version="1.0"?>
<Patient
    xmlns="http://hl7.org/fhir">
    <identifier>
        <gender value="123"/>
        <gender value=""/>
        <gender value=""/>
    </identifier>
</Patient>

1 个答案:

答案 0 :(得分:1)

要使其相对于当前value节点的所需语法是

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

或者,您可以使用绝对表达式

<xsl:value-of select="/ss/active/@value" />

或者,使用变量将其存储在xsl:for-each之外。例如:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
<xsl:output method="xml" indent="yes" />
<xsl:template match="ss">
  <xsl:variable name="active" select="active/@value" />
  <Patient xmlns="http://hl7.org/fhir">
    <identifier>
      <xsl:for-each select="identifier/system/value">
        <xsl:variable name="aa">
          <xsl:if test="@value='10'">
            <xsl:value-of select="$active" />
          </xsl:if>
        </xsl:variable>    
        <gender value="{$aa}"/>         
      </xsl:for-each>        
    </identifier>
  </Patient>
</xsl:template>
</xsl:stylesheet>