This should be simple and everything I read says that select = “name” should select all the nodes with the “name” in an xml document..
So Im trying to work a simple example.. here’s the XML: (its a simple inventory app).
<?xml version="1.0"?>
<inventory type="inventory">
<item type="zone" CanAdd="true">
<title>Building</title>
<item type="porch" CanAdd="true">
<title>Porch</title>
</item>
<item type="receptionRoom" CanAdd="true">
<title>Reception Room</title>
<item type="doorInfo" CanAdd="true">
<title>Door</title>
<item type="doorStyleType" select="multi">
<title>Door Style</title>
<item>
<title>Internal</title>
</item>
<item>
<title>Plain</title>
</item>
</item>
<item type="doorWinMaterialType" select="single">
<title>Door Material</title>
<item type="softwoodColours" select="single">
<title>Softwood - Stained</title>
<item>
<title>Stained Pine</title>
</item>
</item>
</item>
<item type="doorwinFurnitureType" select="multi">
<title>Door Furniture</title>
<item>
<title>Door Handle</title>
</item>
<item>
<title>LetterBox Opening</title>
</item>
</item>
</item>
</item>
</item>
<propertyName><![CDATA[2 Street, Village]]></propertyName>
<propertyRef><![CDATA[15p9]]></propertyRef>
</inventory>
.. and I need to start to process it with XSLT. (In a C# code behind page – that bit works and throws the result into a pop up.)
The XSLT I am playing with is:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<div>
<h2>My Inventory</h2>
<table border="1">
<tr>
<td>
<xsl:value-of select="inventory/propertyName" />
</td>
</tr>
<xsl:for-each select ="item">
<tr>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</table>
<p>Hello world</p>
</div>
</xsl:template>
</xsl:stylsheet>
The “item” node is not found in the for-each loop and I think I’ve hit a mental block as to why. I’m sure the answer is glaringly simple when you know it, but at the moment I don’t and need to solve this before I can move on so to speak.
The output is: (in a browser)..
My Inventory
2 Street, Village
(I expect a list of the "item" node values here in table rows..)
Hello world
Many Thanks,
Brett
The XPath
itemthat you’ve specified for your<xsl:for-each>loop is evaluated relative to the current node, i.e. relative to the document root. In order to select any<item>elements at any nesting depth in the document, you will have to useHowever, I think you want something slightly different, as the text of
<item>elements themselves is their<title>text as well as the text of any nested<item>elements. Possibly, you are looking for:To respond directly to your sentence
This is not correct.
select="name"selects all the elements named “name” in the list of child nodes of the current node, not anywhere in the Xml document.