简体   繁体   English

使用XSL变量获取值(XPath)-xslt

[英]Get Value(XPath) using XSL Variable - xslt

While trying to parse the below xml , 尝试解析以下xml

<root>
<SelectValue>One</SelectValue> <!-- Tends to Vary -->
<SubRoot>  <!-- Iterate elements inside it using [ SelectValue] -->
    <One>One</One>
    <Two>Two</Two>
    <Three>Three</Three> 
</SubRoot>
</root> 

with below xsl , 低于xsl

    <xsl:template match="/root">
        <xsl:variable name="columns" select="SelectValue"/>
        <xsl:for-each select="SubRoot"> -->
                        <tr>
                            <td>
                                <xsl:value-of select="SubRoot/@*[local-name()=$columns]"/>
                            </td>
                        </tr>

  </xsl:for-each>

Retrieves an empty html-tags while i expect something like below, 检索空的html-tags而我期望如下所示,

 <table>
   <thead>
   <th>One</th>
   </thead>
   <tbody>
    <tr>
      <td>one</td>
    </tr>
   <tbody>
</table>

I am trying to pass the value inside <SelectValue> to get the nodes inside <SubRoot> 我试图在<SelectValue>内部传递值以获取<SubRoot>内部的节点

Any thoughts here ? 这里有什么想法吗?

I do not recognize the error message you quote (it doesn't even seem to be English), but I do get an error when trying to run your code. 我无法识别您引用的错误消息(它甚至似乎都不是英语),但是在尝试运行您的代码时确实出现了错误。 The reason is that your variable name is invalid; 原因是您的变量名无效; you need to change: 您需要更改:

<xsl:variable name="$columns" select="..."/>

to: 至:

<xsl:variable name="columns" select="..."/>

Use the $ prefix when referring to the variable, not when defining it. 引用变量时使用$前缀,而不是在定义变量时使用。


Note also that an XPath expression beginning with a / is an absolute path , starting at the root node. 还要注意,以/开头的XPath表达式是绝对路径 ,从根节点开始。 Therefore none of your select expressions - with the exception of /root - will select anything. 因此,除了/root外,所有select表达式都不会选择任何内容。 I am guessing you are trying to do something like: 我猜您正在尝试执行以下操作:

<xsl:template match="/root">
    <xsl:variable name="columns" select="SelectValue"/>
    <tr>
        <td>
            <xsl:value-of select="SubRoot/*[local-name()=$columns]"/>
        </td>
    </tr>
</xsl:template>

which given your input example will return: 给定您的输入示例将返回:

<tr>
  <td>One</td>
</tr>

Within xsl:for-each select="SubRoot", the SubRoot is the context node, so you should not be selecting down to a further SubRoot. 在xsl:for-each select =“ SubRoot”中,SubRoot是上下文节点,因此您不应选择其他SubRoot。 So you want 所以你要

<xsl:for-each select="SubRoot">
     <tr>
          <td>
              <xsl:value-of select="@*[local-name()=$columns]"/>
          </td>
    </tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM