Page 1 of 1

cant reach data in xml with namespaces

Posted: Fri Mar 24, 2006 6:34 pm
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.

Posted: Fri Mar 24, 2006 6:41 pm
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.

Posted: Fri Mar 24, 2006 11:52 pm
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

Posted: Sat Mar 25, 2006 11:25 pm
by TPP
thanks Image