Santhosh
Santhosh

Reputation: 8197

Get Value(XPath) using XSL Variable - xslt

While trying to parse the below 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: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,

 <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>

Any thoughts here ?

Upvotes: 0

Views: 174

Answers (2)

Michael Kay
Michael Kay

Reputation: 163312

Within xsl:for-each select="SubRoot", the SubRoot is the context node, so you should not be selecting down to a further SubRoot. So you want

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

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 116982

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. Therefore none of your select expressions - with the exception of /root - will select anything. 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>

Upvotes: 1

Related Questions