I am trying to access a node inside an .xml file which uses namespaces and sort it. It is not working and I think that it has to do with the namespaces and not being able to qualify them properly.
I have an index.xml which I use to combine the documents that I need and looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="merge.xsl"?>
<pic:catalog xmlns:pic = "pictureCatalog">
<pic:logo>Logo</pic:logo>
<Author>User Name</Author>
<pic:allPhotos>photos</pic:allPhotos>
</pic:catalog>
the photos.xml looks like this:
<?xml version="1.0"?>
<pic:photoCatalog xmlns:pic="pictureCatalog">
<pic:photo>
<pic:title>Alcazar</pic:title>
<pic:location>Segovia - Spain</pic:location>
<pic:date>Jan 2013</pic:date>
<pic:camera>Sony</pic:camera>
<pic:resolution>12px</pic:resolution>
<pic:format>.jpg</pic:format>
<pic:description>
Medieval Castle over the hill overlooking the city.
</pic:description>
</pic:photo>
</pic:photoCatalog>
And my xsl stylesheet looks like this:
<!-- All the photos-->
<xsl:template match = "pic:catalog/pic:allPhotos">
<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css" />
</head>
<body>
<xsl:for-each select="pic:photoCatalog/pic:photo"><br/>
<xsl:sort select="pic:location"/>
<xsl:value-of select="pic:photoCatalog/pic:photo/pic:location"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
Can anyone help?
Bluetxxth
You have two XML files here, but your XSLT is only applied to index.xml, with no reference to photos.xml anywhere. You would normally excpect to see a document reference in the XSLT if you want to access a second XML document.
It looks like the second file name is held in the pic:allPhotos element of index.xml, and that you want to access that file and iterate over the photos. In this case, you need to change your xsl:for-each to access the document like so
The next issue is that you are outputting a br element at this point, before the xsl:sort statement. This is not valid as the xsl:sort should immediately follow the follow the xsl:for-each.
Also, your xsl:value-of is not quite right. As the point this is called, you are in the xsl:for-each loop, and so positioned on a pic:photo element already, so it can be just simplified to this
Try the following XSLT
Which outputs the following
Your namespaces are all present and correct, by the way! (Well, assuming you have declared it corrected on the xsl:stylesheet too)