Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 从其他模板继承变量_Xml_Xslt_Xslt 1.0_Xslt 2.0 - Fatal编程技术网

Xml 从其他模板继承变量

Xml 从其他模板继承变量,xml,xslt,xslt-1.0,xslt-2.0,Xml,Xslt,Xslt 1.0,Xslt 2.0,我正在尝试将变量从上一个模板继承到当前模板 这是我的xsl,想知道是否有什么地方出错: <xsl:template match="child1"> <xsl:variable name="props-value"> <xsl:value-of select="VALUE1"/> </xsl:variable> <xsl:apply-templates select="attribute[matche

我正在尝试将变量从上一个模板继承到当前模板

这是我的xsl,想知道是否有什么地方出错:

<xsl:template match="child1">
    <xsl:variable name="props-value">
        <xsl:value-of select="VALUE1"/>
    </xsl:variable>  
    <xsl:apply-templates select="attribute[matches(.,'=@')]">
        <xsl:with-param name="props-value" select="$props-value" /> 
    </xsl:apply-templates>
</xsl:template>  
<xsl:template match="attribute[matches(.,'=@')]">
<xsl:param name="props-value"/>
<xsl:copy>  
<xsl:apply-templates select="@*"/>
    <xsl:if test="$props_value = 'VALUE1'">
        Value is true
    </xsl:if>
</xsl:copy>
</xsl:template>

价值是真实的

预期输出:值为true。

XSLT存在两个问题:

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

  <xsl:template match="child1">
    <xsl:variable name="props-value">
      <xsl:value-of select=" 'VALUE1' "/>
    </xsl:variable>
    <xsl:apply-templates select="attribute">
      <xsl:with-param name="props-value" select="$props-value" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="attribute">
    <xsl:param name="props-value"/>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:if test="$props-value = 'VALUE1'">
        Value is true
      </xsl:if>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
  • 在第一个模板中的变量中,您选择了
    “VALUE1”
    作为值。这与
    元素匹配。我相信您希望选择
    “'VALUE1'”
    (一个值为'VALUE1'的字符串)
  • 在第二个模板中的测试中,您使用下划线编写了
    $props_value
    ,而参数使用连字符调用
    props value
  • 以下是XSLT的更正版本:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    
      <xsl:template match="child1">
        <xsl:variable name="props-value">
          <xsl:value-of select=" 'VALUE1' "/>
        </xsl:variable>
        <xsl:apply-templates select="attribute">
          <xsl:with-param name="props-value" select="$props-value" />
        </xsl:apply-templates>
      </xsl:template>
    
      <xsl:template match="attribute">
        <xsl:param name="props-value"/>
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:if test="$props-value = 'VALUE1'">
            Value is true
          </xsl:if>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    
    
    价值是真实的
    
    应用于以下输入XML时:

    <child1>
      <attribute/>
    </child1>
    
    
    
    它生成以下输出XML:

    <attribute>
                Value is true
              </attribute>
    
    
    价值是真实的
    
    请提供任何建议。。。