【问题标题】:Getting Value from XML and Store In Variable Using XSLT使用 XSLT 从 XML 中获取值并存储在变量中
【发布时间】:2013-03-14 16:23:18
【问题描述】:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<Result>
  <resultDetails>
    <resultDetailsData>
      <itemProperties>
        <ID>1</ID>
        <type>LEVEL</type> 
        <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">5</value> 
      </itemProperties>
    </resultDetailsData>
  </resultDetails>
</Result>

我有上面描述的xml。我想使用类型标签的值(即本例中的 LEVEL)获取值标签的值(在本例中为“5”),并使用 XSLT 将其存储在变量中,以便我可以使用该变量稍后。

我该怎么做?

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    你可以这样做:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0">
    
    <xsl:template match="/">
        <xsl:variable name="myVar" select="Result/resultDetails/resultDetailsData/itemProperties/value"/>
    <varoutput>
        <xsl:value-of select="$myVar"/>
    </varoutput>
    </xsl:template> 
    
    </xsl:stylesheet>
    

    应用到您的输入 XML,您会得到以下输出:

    <?xml version="1.0" encoding="UTF-8"?>
    <varoutput>5</varoutput>
    

    【讨论】:

      【解决方案2】:

      如果你想使用读取变量来设置一个属性(即一行的颜色)你需要使用 {$variable} 如下

      <xsl:variable name="rColor" select="rowColor"/>
      

      然后

      <fo:table-row background-color="{$rColor}">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-15
        相关资源
        最近更新 更多