Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt: value of text associated with element

Tags:

xml

xslt

If I have an XML file that includes

<param name="foo" value="5000" >foo is a way of making pasta sauce</param>
<param name="bar" value="3000" >bar is controlling the beer taps</param>

and I want to use XSLT to process this into an HTML file, with the name and value attributes and the text as a description, how can I get the XML node text?

<xsl:for-each select="param">
   <tr>
      <td><xsl:value-of select="@name"/></td>
      <td><xsl:value-of select="@value"/></td>
      <td><xsl:text> </xsl:text></td>
   </tr>
</xsl:for-each>

The above XSLT fragment does successfully get the name and value attributes, but it fails to get the text, and I think I'm missing something obvious but I don't know what.

like image 905
Jason S Avatar asked Aug 12 '10 15:08

Jason S


People also ask

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.

Which symbol is used to get the element value in XSLT?

XSLT <xsl:value-of> Element The <xsl:value-of> element is used to extract the value of a selected node.

What is value of select in XSLT?

The XSLT <xsl:value-of> element is used to extract the value of selected node. It puts the value of selected node as per XPath expression, as text.

Which XSLT element is used to write literal text to the output?

The <xsl:text> element writes literal text to the output tree.


2 Answers

Try this

<xsl:for-each select="param">
   <tr>
      <td><xsl:value-of select="@name"/></td>
      <td><xsl:value-of select="@value"/></td>
      <td><xsl:value-of select="text()"/></td>
   </tr>
</xsl:for-each>
like image 81
YoK Avatar answered Oct 13 '22 08:10

YoK


aha, this also seems to work:

<xsl:for-each select="param">
   <tr>
      <td><xsl:value-of select="@name"/></td>
      <td><xsl:value-of select="@value"/></td>
      <td><xsl:value-of select="."/></td>
   </tr>
</xsl:for-each>
like image 26
Jason S Avatar answered Oct 13 '22 10:10

Jason S