3

I am trying to display an XML tag attribute via xslt but am having trouble figuring it out.

My xml file looks similar to this:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="results.xsl"?>
<search command="grep -n -i -I htm C:\firebreath\*">
<match number="1">
    <filename>C:\firebreath\CMakeLists.txt  </filename>
    <linenum>10 </linenum>
    <matchstring>#            http://www.gnu.org/licenses/lgpl-2.1.html</matchstring>
</match>
</search>

And my XSLT file looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>grep matches </h2>
<table border="1">
  <tr bgcolor="#9acd32">
    <th>filename</th>
    <th>line number</th>
    <th>match string</th>
  </tr>
  <xsl:for-each select="search/match">
  <tr>
    <td><xsl:value-of select="filename"/></td>
    <td><xsl:value-of select="linenum"/></td>
    <td><xsl:value-of select="matchstring"/></td>
  </tr>
  </xsl:for-each>
</table>
</body>
</html>

I want to display the command portion of the search tag before I begin to parse through the rest. Is there any way do to that? If so, how? A search didn't turn up anything particularly relevant...

1
  • Good question, +1. See my answer for a complete and short solution. :) Commented Nov 8, 2010 at 19:08

1 Answer 1

5

I want to display the command portion of the search tag before I begin to parse through the rest.

Read about the <xsl:value-of> XSLT instruction.

And use:

 <xsl:value-of select="/search/@command"/>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick response. I guess my problem had to deal with chrome, as it doesn't want to display attributes at all.
@user483281: that shouldn't matter, as long as you're not outputting the value of @command as an attribute. If you use <xsl:value-of >, you're outputting it as a string. I have not heard of Chrome not wanting to display attributes btw... do you mean Google Chrome or Mozilla chrome?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.