tags:

views:

83

answers:

4

Here is a snip-it of the XML:

<?xml version="1.0" encoding="iso-8859-1" ?>
<NetworkAppliance id="S123456">
  <Group id="9">
    <Probe id="1">
      <Value>74.7</Value>
    </Probe>
</NetworkAppliance>

I want to get the single point value of 74.7. There are many groups with unique ID's and many Probes under that group with unique ID's each with values.

I am looking for example XSLT code that can get me this one value. Here is what i have that does not work:

<?xml version="1.0" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" version="3.2" /> 
    <xsl:template match="NetworkAppliance">
        <xsl:apply-templates select="Group[@id='9']"/>
    </xsl:template>
    <xsl:template match="Group">
        Temp: <xsl:value-of select="Probe[@id='1']/Value"/>
        <br/>
    </xsl:template>
</xsl:stylesheet>

Here is what worked for me in the end:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:template match="/">
 <html>
 <body>
     <xsl:for-each select="NetworkAppliance/Group[@id=9]/Probe[@id=1]">
             Value: <xsl:value-of select="Value" />
     </xsl:for-each>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>
A: 

the xpath for value of a node is /node/text()

So

<xsl:value-of select="Probe[@id='1']/text()"/>
dnagirl
where did you saw a CDATA section?
Rubens Farias
Sorry, used the wrong term
dnagirl
A: 

XSLT is just one of the tools in the box, and nothing without XPath.

annakata
+2  A: 

Don't forget that you can do select several levels at once. Fixing your XML to:

<?xml version="1.0" encoding="iso-8859-1" ?>
<NetworkAppliance id="S123456">
  <Group id="9">
    <Probe id="1">
      <Value>74.7</Value>
    </Probe>
  </Group>
</NetworkAppliance>

and using this stylesheet:

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" version="3.2" /> 

  <xsl:template match="/">
    Temp: <xsl:value-of select="//Group[@id='9']/Probe[@id='1']/Value"/>
    <br/>
  </xsl:template>
</xsl:stylesheet>

we can pick out that one item you're interested in.

Points to note:

  • The // part of the expression means that the search for Group nodes takes place throughout the whole tree, finding Group nodes at whatever depth they're at.
  • The [@id='9'] part selects those Group nodes with id of 9
  • The Probe[@id='1'] part immediately after that selects those children of the Group nodes it found where the id is 1, and so on.
Tim
A: 
<xsl:value-of select="/NetworkAppliance/Group[@id=9]/Probe[@id=1]/Value"/>
Marc Gravell