Re: [xsl] trying to figure out handling namespace issues

Subject: Re: [xsl] trying to figure out handling namespace issues
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 12 Dec 2008 15:53:28 -0500
At 2008-12-12 12:20 -0800, Fred Christian wrote:
I continue to have problems getting my brain to understand how to handle namespace problems. I think I have broken one down simple enough to get help on. (Using Saxon 9)
Basically I have this xsl:
<xsl:for-each select="child::*[1]">
3)<xsl:value-of select="name(.)"/>#
</xsl:for-each>
<xsl:for-each select="div">
4)<xsl:value-of select="name(.)"/>#
</xsl:for-each>


and this output:
   3)div#

---
The problem, there is no "4)div#"
I realize it is because of a namespace problem, but I don't know how to solve it. Help?

Unprefixed names in XPath by default are names in no namespace, not names in the default namespace. You've declared the XHTML to be the default namespace.


1. Is there a way that I can write some xsl, that will give me temporary output so I can figure out what I need to add to my xsl so that "4)div#" will work without looking at the xml? (I ask this, because sometimes the xml I am working with is huge and it is easy to get lost).

2. Following are the files I am using, stripped down as much as I dared. How can I modify my XSL so the "4)div#" works with a direct select statement instead of *[1]? (because it won't always be in the first position)

Two mutually exclusive solutions: (1) associate the XHTML namespace with a prefix and use the prefix in your select, or (2) declare the default XPath namespace as the XHTML namespace (because you are using XSLT 2 and not XSLT 1).


But I just realized that (2) is not available to you because your XML element names are in no namespace and you need to match those, so you are stuck with (1).

________ XSL file ____________________
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns="http://www.w3.org/1999/xhtml";
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
exclude-result-prefixes="xsl">
...
                  <xsl:for-each select="div">
                    4)<xsl:value-of select="name(.)"/>#

Change that as follows:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:h="http://www.w3.org/1999/xhtml";
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
...
                  <xsl:for-each select="h:div">
                    4)<xsl:value-of select="name(.)"/>#

If solution (2) were available to you it would look like the following:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xpath-default-namespace="http://www.w3.org/1999/xhtml";>
...
                  <xsl:for-each select="div">
                    4)<xsl:value-of select="name(.)"/>#

.... but it would interfere with your match= of the no-namespace elements in your XML file, so this approach is not easily available to you. It is available to you if you declare xpath-default-namespace= in multiple places in your stylesheet, but that gets awkward.

Note in both cases you do not need to exclude the XSLT namespace as that is implicit.

I hope this helps.

. . . . . . . . . Ken

--
Upcoming XSLT/XSL-FO, UBL and code list hands-on training classes:
:  Sydney, AU 2009-01/02; Brussels, BE 2009-03; Prague, CZ 2009-03
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video sample lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg
Video course overview:  http://www.youtube.com/watch?v=VTiodiij6gE
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread