Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

498
Views
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 ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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>
over 4 years ago · Santiago Trujillo Report

0

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>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!