PDA

View Full Version : XML+XSLT get child name


pdaniel2006
10-03-06, 09:58 AM
This are my files :

dtest.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="dtest.xsl"?>
<masini>
<model>
<producator>AUDI</producator>
<motor>100CP</motor>
<vitezaMaxima>300km/h</vitezaMaxima>
<pret>20</pret>
</model>
<model>
<producator>BMW</producator>
<motor>1000CP</motor>
<vitezaMaxima>500Km/h</vitezaMaxima>
<pret>151</pret>
</model>
<model>
<producator>Renault</producator>
<motor>0,9Cp</motor>
<vitezaMaxima>12km/h</vitezaMaxima>
<pret>10</pret>
</model>
<model>
<producator>VW</producator>
<motor>100CP</motor>
<vitezaMaxima>500Km/h</vitezaMaxima>
<pret>15</pret>
</model>
</masini>


dtest.xsl


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>

<!--<xsl:for-each select="masini/model">-->
<xsl:apply-templates />
<!-- <xsl:value-of select="producator" />-->
<!--</xsl:for-each>-->


<xsl:for-each select="masini/model">
<xsl:choose>
<xsl:when test="pret mod 2 = 0">
<tr bgcolor="red">
<td>
<xsl:value-of select="producator"/>
</td>
<td>
<xsl:value-of select="motor"/>
</td>
<td>
<xsl:value-of select="vitezaMaxima"/>
</td>
<td>
<xsl:value-of select="pret"/>
</td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr bgcolor="c1c1c1">
<td>
<xsl:value-of select="producator"/>
</td>
<td>
<xsl:value-of select="motor"/>
</td>
<td>
<xsl:value-of select="vitezaMaxima"/>
</td>
<td>
<xsl:value-of select="pret"/>
</td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="masini">
<tr>
<xsl:apply-templates />
</tr>
</xsl:template>
<xsl:template match="model">
<td>

<xsl:value-of select="name(child::node())" />

</td>
</xsl:template>
</xsl:stylesheet>



How you can see, with this expresion : <xsl:value-of select="name(node())" /> i take the child from model node. The problem is that it takes me only the first childe name, in my case "producator". I whant to take all the childrens names of that node (producator,motor,vitezaMaxima,pret). I have tried a for-each but doesen't work. Any ideas?

pdaniel2006
10-03-06, 11:21 AM
And also another think that i have to do is this :


<td style="padding-right:3px; background-color:blue; color:white; cursor:pointer" onclick="getData('<xsl:value-of select"link"')"><xsl:value-of select="name" /></td>


How can i put that value into the javascript function attribute? This method, that i have used does not work. Thx.

UnrealEd
10-03-06, 12:09 PM
i don't have my xml book here, so i can't help you with your first question.
with the second question: here's how you have to do it:

<td style="padding-right:3px; background-color:blue; color:white; cursor:pointer" onclick="getData('&lt;xsl:value-of select=\'link\'&gt;')"><xsl:value-of select="name" /></td>
hope this helps you any further,
UnrealEd

pdaniel2006
10-04-06, 02:56 AM
Nope, this method does not work. This is my HTML outpout:


<tr>
<td style="padding-right:3px; background-color:blue; color:white; cursor:pointer" onclick="getData1('&lt;xsl:value-of select=\'link\'&gt;')">Primul Tab</td>
<td style="padding-right:3px; background-color:blue; color:white; cursor:pointer" onclick="getData1('&lt;xsl:value-of select=\'link\'&gt;')">Al doilea tab</td>
<td style="padding-right:3px; background-color:blue; color:white; cursor:pointer" onclick="getData1('&lt;xsl:value-of select=\'link\'&gt;')">Al treilea tab</td>
</tr>


So you can see that it is taking me the "getData1('&lt;xsl:value-of select=\'link\'&gt;')" not the value of &lt;xsl:value-of select=\'link\'&gt;. Do you have other idea?

UnrealEd
10-04-06, 05:34 PM
Hmmm, seems like i gave you the wrong sollution :)
try this one:

<td style="padding-right:3px; background-color:blue; color:white; cursor:pointer" onclick="getData('&lt;xsl:value-of select=\"link\" /&gt;')"><xsl:value-of select="name" /></td>
as xsl:value-of is an empty node, you have to place an empty tag:
<xsl:value-of select="something" />

Greetz,
UnrealEd

pdaniel2006
10-05-06, 03:05 AM
No, this does not work. I have tried this before. This is the error

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

Required white space was missing. Error processing resource 'http://127.0.0.1/xslt/taburi/taburi.xslt'. Line 120, Position...

<td style="padding-right:3px; background-color:blue; color:white; cursor:pointer" onclick="getData('&lt;xsl:value-of ...