XSLT node Traversal

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">
<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>
13.10.2009 19:09:12
what does your current code return?
dnagirl 13.10.2009 19:11:54
running your code (and fixing a missing tag in your XML) I got following result: "Temp: 74.7"
Rubens Farias 13.10.2009 19:16:58
4 ОТВЕТА
РЕШЕНИЕ

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.
4
13.10.2009 20:03:51

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

So

<xsl:value-of select="Probe[@id='1']/text()"/>
0
13.10.2009 20:02:05
where did you saw a CDATA section?
Rubens Farias 13.10.2009 19:18:32

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

0
13.10.2009 19:18:21
<xsl:value-of select="/NetworkAppliance/Group[@id=9]/Probe[@id=1]/Value"/>
1
13.10.2009 20:04:33