Transform fails when xmlns namespace declaration present

Here should go questions about transforming XML with XSLT and FOP.
mmiller
Posts: 2
Joined: Mon Sep 26, 2005 9:32 pm

Transform fails when xmlns namespace declaration present

Post by mmiller » Tue Sep 27, 2005 7:48 pm

I have a DSML document that declares a default namespace. When the xmlns attribute is present, the transform does not return the desired results. When I remove the attribute, the transform works as expected.

I am trying to learn what I am not understanding about the roll of the namespace declaration relative to what the transform process is needing.

The namespace declaration is present by default as I am using a binary DSML toolset distribution that always includes it. Note that the URL http://www.dsml.org/DSML returns a 404 as dsml.org is no longer maintained.

I appreciate any suggestions that help me understand how the namespace declaration is impacting this simple transform process.

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<dsml complete="false" xmlns="http://www.dsml.org/DSML">
<directory-entries>
<entry dn="dc=qua,dc=company,dc=com">
<objectclass>
<oc-value>top</oc-value>
<oc-value>domain</oc-value>
</objectclass>
<attr name="dc">
<value>qua</value>
</attr>
</entry>
<entry dn="cn=Directory Administrators, dc=qua,dc=company,dc=com">
<objectclass>
<oc-value>top</oc-value>
<oc-value>groupofuniquenames</oc-value>
</objectclass>
<attr name="cn">
<value>Directory Administrators</value>
</attr>
</entry>
<entry dn="ou=Groups, dc=qua,dc=company,dc=com">
<objectclass>
<oc-value>top</oc-value>
<oc-value>organizationalunit</oc-value>
</objectclass>
<attr name="ou">
<value>Groups</value>
</attr>
</entry>
<entry dn="ou=People, dc=qua,dc=company,dc=com">
<objectclass>
<oc-value>top</oc-value>
<oc-value>organizationalunit</oc-value>
</objectclass>
<attr name="ou">
<value>People</value>
</attr>
</entry>
</directory-entries>
</dsml>
The stylesheet is as follows:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="entry">
<dn>
<xsl:value-of select="./@dn"/>
</dn>
</xsl:template>
</xsl:stylesheet>
Successful output:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<dn>dc=qua,dc=company,dc=com</dn>
<dn>cn=Directory Administrators, dc=qua,dc=company,dc=com</dn>
<dn>ou=Groups, dc=qua,dc=company,dc=com</dn>
<dn>ou=People, dc=qua,dc=company,dc=com</dn>

george
Site Admin
Posts: 2100
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Tue Sep 27, 2005 7:56 pm

Hi,

When you say
<xsl:template match="entry">
that means the template will match on entry elements from no namespace.
If you do not have the default namespace declaration in your XML file then the entry elements belong to no namespace and are matched by your template. If you add the default namespace declaration xmlns="http://www.dsml.org/DSML" then the entry elements belong to the "http://www.dsml.org/DSML" namespace and are not matched anymore by your template. You need to declare the "http://www.dsml.org/DSML" in your stylesheet using a prefix (note that XPath does not handle default namespace so you need a prefix) and use that to qualify the entry element in the match attribute, something like:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:d="http://www.dsml.org/DSML">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="d:entry">
<dn>
<xsl:value-of select="./@dn"/>
</dn>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George

mmiller
Posts: 2
Joined: Mon Sep 26, 2005 9:32 pm

Post by mmiller » Tue Sep 27, 2005 8:21 pm

George,

Thanks for your clear response. I will have to get the source for the package I am using and tweak the namespace to play better with XPATH.

Greatly appreciate your generosity.

Mark

Post Reply