xslt - Can't retrieve info from xml document with xsl -


i'm starting learn xml , xsl, reason can't make xpath work me in xsl. xml document structured in wrong way or why isn't price of game 14 displayed in table?

my xsl attempt pretty simple:

<?xml version="1.0" encoding="iso-8859-1"?> <!-- edited xmlspy® --> <xsl:stylesheet version="1.0"                 xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:template match="/">   <html>   <body>   <h2>my games</h2>     <table border="1">       <tr bgcolor="#9acd32">         <th>game name</th>         <th>price</th>       </tr>       <tr>         <td><xsl:value-of select=             "xboxspellen/spellen/spel[14]/spelnaam"/></td>         <td><xsl:value-of select=             "xboxspellen/spellen/spel[14]/prijs"/></td>       </tr>     </table>   </body>   </html> </xsl:template> </xsl:stylesheet> 

shorter version of xml code:

<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="xboxspellen.xsl"?> <xboxspellen xmlns="http://www.w3schools.com"               xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"               xsi:schemalocation="http://www.w3schools.com                                   xboxspellen.xsd">   <genres>     ...     <genre id="adv">       <genrenaam>adventure</genrenaam>       <spel idref="12"/>       <spel idref="13"/>       <spel idref="14"/>     </genre>   </genres>    <spellen>     ...     ...     <spel id="14">       <spelnaam>omerta: city of gangsters</spelnaam>       <uitgever>kalypso</uitgever>       <prijs>49.98</prijs>       <leeftijd>16</leeftijd>       <aankoop>         <dag>28</dag>         <maand>04</maand>         <jaar>2011</jaar>       </aankoop>       <afbeelding>spel14.jpg</afbeelding>     </spel>   </spellen> </xboxspellen> 

i've been trying things hours don't seem find answer, if me, i'd thankful :d

you've made start. , you've bumped shins on classic slip happens sooner or later: you've written xpath expression xboxspellen/spellen/spel[14]/spelnaam , expect match element in document. elements in document not named xboxspellen, etc.: namespace-qualified, , of them have default namespace declaration (xmlns="...") in scope, full (expanded) name not xboxspellen etc., {http://www.w3schools.com}xboxspellen, {http://www.w3schools.com}spellen, etc.

correct situation making xpath expressions , xml agree on names of things. in usual case, means:

  1. add namespace declaration xslt stylesheet, binding prefix (it doesn't matter prefix) namespace used in xml:

    xmlns:w3s = "http://www.w3schools.com"

  2. use prefix in xpath expressions:

    <td><xsl:value-of select="w3s:xboxspellen                           /w3s:spellen                           /w3s:spel[14]                           /w3s:spelnaam" /></td> <td><xsl:value-of select="w3s:xboxspellen                           /w3s:spellen                           /w3s:spel[14]                           /w3s:prijs"/></td> 

(i've broken lines in xpath legibility; white space legal in xpath expressions, , it's thing, too.)

now xpath expression match.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -