cant reach data in xml with namespaces

Here should go questions about transforming XML with XSLT and FOP.
TPP
Posts: 5
Joined: Fri Mar 24, 2006 6:32 pm

cant reach data in xml with namespaces

Post by TPP »

Hi!
I have a problem retriving data from xml file.

XML:

Code: Select all


<?xml version="1.0" encoding="utf-8" ?> 
<Data xmlns="http://something.com/Documents/Schemas/doc.xsd"
xmlns:e="http://something.com/Documents/Schemas/file.xsd">
<e:Head>
<e:name>John</e:name>
<e:address1>London, UK</e:address1>
<e:postNumber>123456</e:postNumber>
</e:Head>
<body>
<age>35</age>
<kids>3</kids>
<car>ford</car>
</body>
</Data>

XSLT:

Code: Select all


<?xml version="1.0" encoding="windows-1250" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:edp="http://http://something.com/Documents/Schemas/file.xsd">
<xsl:template match="/">
<xsl:value-of select="//Data/e:Head/e:name" />
<xsl:value-of select="//Data/Body/age" />
</xsl:template>
</xsl:stylesheet>

...why do i get blank output?

Thank you for your help.
TPP
Posts: 5
Joined: Fri Mar 24, 2006 6:32 pm

Post by TPP »

in posted xslt there is a typing error in 3rd line:
xmlns:edp="http://http://something.com/Documents/Schemas/file.xsd">

should be:
xmlns:e="http://http://something.com/Documents/Schemas/file.xsd">

...this does not solve the problem from the first post.

Thanks again for your help.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi,

You have a default namespace declared in your Data element and that makes all the elements without a prefix to belong to that default namespace. On the other hand in the stylesheet you match on similar local names as the names of the elements from the default namespce in your document but you match elements from no namespace. The solution is to define your document default namespace also in the stylesheet using some prefix and then use that prefix to qualify the element names in order to correclty refer to those elements from their namespace.
Also note that XML is case sensitive so body and Body are different things.
Bellow you can find a working stylesheet:

Code: Select all


<?xml version="1.0" encoding="windows-1250" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:e="http://something.com/Documents/Schemas/file.xsd"
xmlns:d="http://something.com/Documents/Schemas/doc.xsd">
<xsl:template match="/">
<xsl:value-of select="//d:Data/e:Head/e:name" />
<xsl:value-of select="//d:Data/d:body/d:age" />
</xsl:template>
</xsl:stylesheet>
Best Regards,
George
TPP
Posts: 5
Joined: Fri Mar 24, 2006 6:32 pm

Post by TPP »

thanks Image
Post Reply