namespace problem with XSLT

Discussion in 'XML' started by Bernd Fuhrmann, Sep 30, 2003.

  1. Hi!

    I have some trouble with some simple stupid XSLT-stuff.

    My stylesheet:
    -------------
    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml"
    >


    <xsl:eek:utput method="xml"
    cdata-section-elements="script"
    doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    encoding="iso-8859-1"/>

    <xsl:template match="html">
    <p>Inserted</p>
    </xsl:template>

    </xsl:stylesheet>
    -------------
    My XML-Document:
    -------------
    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>Whatever</title></head><body></body></html>
    -------------

    The result of this transformation is:
    -------------
    <?xml version="1.0" encoding="iso-8859-1"?>
    -------------
    or
    -------------
    <?xml version="1.0" encoding="iso-8859-1"?>

    Whatever
    -------------

    Obviously the result should be sth. like <?xml ...><p>Inserted</p> and
    now for the truly strange thing: Changing the stylesheet to:
    ------------
    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    >


    <xsl:eek:utput method="xml"
    cdata-section-elements="script"
    doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    encoding="iso-8859-1"/>

    <xsl:template match="xhtml:html">
    <p>Inserted</p>
    </xsl:template>

    </xsl:stylesheet>
    ------------
    results in:
    ------------
    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE p PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <p xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">Inserted</p>
    ------------

    Why? Why does changing a namespace declaration change the behaviour of a
    XSLT processor (tried Saxon (7.6.5) and Xalan (1.6.0 with Xerxes 2.3.0))
    in that way? Shouldn't match="sometag" mean default-xmlns:sometag?

    In case the xmlns:xhtml-Namespace is necessary: Is there a way to keep
    the xslt-processors (saxon or xalan) from inserting unused, thus
    unnecessary namespace declarations that interfere with the W3C XHTML
    validator?

    Thanks in advance
    Bernd Fuhrmann
     
    Bernd Fuhrmann, Sep 30, 2003
    #1
    1. Advertising

  2. This is a very FAQ.

    See:

    http://www.topxml.com/people/bosley/defaultns.asp


    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL


    "Bernd Fuhrmann" <> wrote in message
    news:blcbnk$h4f$02$-online.com...
    > Hi!
    >
    > I have some trouble with some simple stupid XSLT-stuff.
    >
    > My stylesheet:
    > -------------
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <xsl:stylesheet
    > version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > xmlns="http://www.w3.org/1999/xhtml"
    > >

    >
    > <xsl:eek:utput method="xml"
    > cdata-section-elements="script"
    > doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    > doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    > encoding="iso-8859-1"/>
    >
    > <xsl:template match="html">
    > <p>Inserted</p>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    > -------------
    > My XML-Document:
    > -------------
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <head><title>Whatever</title></head><body></body></html>
    > -------------
    >
    > The result of this transformation is:
    > -------------
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > -------------
    > or
    > -------------
    > <?xml version="1.0" encoding="iso-8859-1"?>
    >
    > Whatever
    > -------------
    >
    > Obviously the result should be sth. like <?xml ...><p>Inserted</p> and
    > now for the truly strange thing: Changing the stylesheet to:
    > ------------
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <xsl:stylesheet
    > version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > xmlns="http://www.w3.org/1999/xhtml"
    > xmlns:xhtml="http://www.w3.org/1999/xhtml"
    > >

    >
    > <xsl:eek:utput method="xml"
    > cdata-section-elements="script"
    > doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    > doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    > encoding="iso-8859-1"/>
    >
    > <xsl:template match="xhtml:html">
    > <p>Inserted</p>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    > ------------
    > results in:
    > ------------
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <!DOCTYPE p PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    > <p xmlns="http://www.w3.org/1999/xhtml"
    > xmlns:xhtml="http://www.w3.org/1999/xhtml">Inserted</p>
    > ------------
    >
    > Why? Why does changing a namespace declaration change the behaviour of a
    > XSLT processor (tried Saxon (7.6.5) and Xalan (1.6.0 with Xerxes 2.3.0))
    > in that way? Shouldn't match="sometag" mean default-xmlns:sometag?
    >
    > In case the xmlns:xhtml-Namespace is necessary: Is there a way to keep
    > the xslt-processors (saxon or xalan) from inserting unused, thus
    > unnecessary namespace declarations that interfere with the W3C XHTML
    > validator?
    >
    > Thanks in advance
    > Bernd Fuhrmann
    >
     
    Dimitre Novatchev, Sep 30, 2003
    #2
    1. Advertising

  3. Dimitre Novatchev wrote:

    > See:
    > http://www.topxml.com/people/bosley/defaultns.asp

    Read. However that wasn't obvious from reading the standard. At least I
    have not found any part in XSLT/XPath/... 1.0 that talks about this problem.

    > This is a very FAQ.


    Possible. I have, so far, only read some examples and done some stuff
    myself. So I must write <xsl:template match="xhtml:sometag">... . But
    how can I then avoid to break validation of XHTML by their DTDs when
    Xalan and Saxon (and most likely some other XSLT processors too) insert
    their redundant xmlns:xhtml="..." in the result?

    Thanks again in advance,
    Bernd Fuhrmann
     
    Bernd Fuhrmann, Sep 30, 2003
    #3
  4. Dimitre Novatchev wrote:
    > Can you give an example?

    Sure:
    Stylesheet:
    -----------
    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    >


    <xsl:eek:utput method="xml"
    cdata-section-elements="script"
    doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    encoding="iso-8859-1"/>

    <xsl:template match="*|@*|text()|processing-instruction()">
    <xsl:copy>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="xhtml:html/xhtml:head/xhtml:title">
    <title>Some Title</title>
    </xsl:template>

    </xsl:stylesheet>
    -----------
    XML-Document:
    -----------
    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>Whatever</title></head><body></body></html>
    -----------
    Result:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title xmlns:xhtml="http://www.w3.org/1999/xhtml">Some
    Title</title></head><body /></html>
    -----------
    The result actually changes a bit (but not to any solution) when I use
    <xhtml:title>Some Title</xhtml:title>.
    Both, Xalan and Saxon give me a similar result. Of course this will
    never validate to the XHTML-DTD.

    Any idea what I could do to fix this problem?

    Thanks in advance,
    Bernd Fuhrmann
     
    Bernd Fuhrmann, Sep 30, 2003
    #4
  5. > So I must write <xsl:template match="xhtml:sometag">... . But
    > how can I then avoid to break validation of XHTML by their DTDs when
    > Xalan and Saxon (and most likely some other XSLT processors too) insert
    > their redundant xmlns:xhtml="..." in the result?



    Can you give an example?


    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL
     
    Dimitre Novatchev, Sep 30, 2003
    #5
  6. In article <blcorj$5ua$00$-online.com>, Bernd Fuhrmann wrote:
    [snip]
    > Possible. I have, so far, only read some examples and done some stuff
    > myself. So I must write <xsl:template match="xhtml:sometag">... . But
    > how can I then avoid to break validation of XHTML by their DTDs when
    > Xalan and Saxon (and most likely some other XSLT processors too) insert
    > their redundant xmlns:xhtml="..." in the result?


    Add

    exclude-result-prefixes="xhtml"

    as an attribute of xsl:stylesheet.
     
    A. Bolmarcich, Oct 1, 2003
    #6
  7. Using
    exclude-result-prefixes="xhtml"

    on the xsl:stylesheet element

    I get a good result from XalanJ 2.1.4

    but with Saxon 6.5.2 still the prefix is on the title element.

    File this as a Saxon bug (the latest version is 6.5.3 -- probably this is
    fixed there?)

    or
    use Saxon 7, which also produces the correct result.


    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL


    "Bernd Fuhrmann" <> wrote in message
    news:blcq1e$o6b$01$-online.com...
    > Dimitre Novatchev wrote:
    > > Can you give an example?

    > Sure:
    > Stylesheet:
    > -----------
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <xsl:stylesheet
    > version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > xmlns="http://www.w3.org/1999/xhtml"
    > xmlns:xhtml="http://www.w3.org/1999/xhtml"
    > >

    >
    > <xsl:eek:utput method="xml"
    > cdata-section-elements="script"
    > doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    > doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    > encoding="iso-8859-1"/>
    >
    > <xsl:template match="*|@*|text()|processing-instruction()">
    > <xsl:copy>
    > <xsl:apply-templates select="*|@*|text()|processing-instruction()"/>
    > </xsl:copy>
    > </xsl:template>
    >
    > <xsl:template match="xhtml:html/xhtml:head/xhtml:title">
    > <title>Some Title</title>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    > -----------
    > XML-Document:
    > -----------
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <head><title>Whatever</title></head><body></body></html>
    > -----------
    > Result:
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <head><title xmlns:xhtml="http://www.w3.org/1999/xhtml">Some
    > Title</title></head><body /></html>
    > -----------
    > The result actually changes a bit (but not to any solution) when I use
    > <xhtml:title>Some Title</xhtml:title>.
    > Both, Xalan and Saxon give me a similar result. Of course this will
    > never validate to the XHTML-DTD.
    >
    > Any idea what I could do to fix this problem?
    >
    > Thanks in advance,
    > Bernd Fuhrmann
    >
     
    Dimitre Novatchev, Oct 1, 2003
    #7
  8. Dimitre Novatchev wrote:
    > Using
    > exclude-result-prefixes="xhtml"
    >
    > on the xsl:stylesheet element
    >
    > I get a good result from XalanJ 2.1.4
    >
    > but with Saxon 6.5.2 still the prefix is on the title element.
    >
    > File this as a Saxon bug (the latest version is 6.5.3 -- probably this is
    > fixed there?)
    >
    > or
    > use Saxon 7, which also produces the correct result.

    I'm using Saxon 7 and/or Xalan. It works, thanks a lot. Thanks aswell to
    A. Bolmarcich!
    Bernd Fuhrmann
     
    Bernd Fuhrmann, Oct 4, 2003
    #8
  9. Bernd Fuhrmann

    Greg Guest

    Sorry if I'm not posting to the right place.

    I have an unwanted "xmlns" attribute showing up in my otherwise-valid
    XHTML result document from an XSLT transformation.

    The source XML document uses XHTML elements. To avoid prefixing them,
    I have made "http://www.w3.org/1999/xhtml" the default namespace.

    <?xml version="1.0" encoding="UTF-8"?>
    <myml:thought
    xmlns:myml="http://www.mywords.com"
    xmlns="http://www.w3.org/1999/xhtml">

    <myml:translation xml:lang="fr">
    <p>
    Tu es <em>belle</em>.
    </p>
    </myml:translation>

    </myml:thought>


    In XSLT style sheet, I again make "http://www.w3.org/1999/xhtml" the
    default namespace. Notice that I have using the XLST "copy-of" to get
    the XHTML elements from the XML source document into the result
    document.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:myml="http://www.mywords.com"
    xmlns="http://www.w3.org/1999/xhtml"
    >


    <xsl:eek:utput
    method="xml"
    version="1.0"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"

    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    indent="yes"
    media-type="application/xhtml+xml"
    />

    <xsl:template match="myml:thought">
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <xsl:apply-templates select="myml:translation"/>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="myml:translation">
    <div>
    <xsl:attribute name="xml:lang">
    <xsl:value-of select="@xml:lang"/>
    </xsl:attribute>
    <xsl:copy-of select="./*"/>
    </div>
    </xsl:template>

    </xsl:stylesheet>


    The result I would hope for is this, a valid XHTML 1.0 Strict web page:


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title />
    </head>
    <body>
    <div xml:lang="fr">
    <p>
    Tu es <em>belle</em>.
    </p>
    </div>
    </body>
    </html>


    But the result I get is an invalid XHTML 1.0 Strict web page:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:myml="http://www.mywords.com">
    <head>
    <title />
    </head>
    <body>
    <div xml:lang="fr">
    <p>
    Tu es <em>belle</em>.
    </p>
    </div>
    </body>
    </html>

    Note that the opening html tag of this result includes a "xmlns:myml"
    which I don't think I need. Is there any way to make sure that it
    doesn't get into the result document?

    I'm using Ant 1.6.2 to make the transformation (so Xalan 2.6.0?).
    Thanks,

    Greg.
     
    Greg, Dec 14, 2004
    #9
  10. Bernd Fuhrmann

    Joris Gillis Guest

    > I have an unwanted "xmlns" attribute showing up in my otherwise-valid
    > XHTML result document from an XSLT transformation.

    Hi,

    it's actually quite simple:
    just add the 'exclude-result-prefixes' attribute to the 'xsl:stylesheet' element:

    <xsl:stylesheet exclude-result-prefixes="myml"/>

    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Spread the wiki (http://en.wikipedia.org)
     
    Joris Gillis, Dec 14, 2004
    #10
  11. copy-of copies the node with all its in scope namespaces, which you
    don't want here, so instead do something like

    <xsl:template match="myml:translation">
    <div>
    <xsl:copy-of select="@xml:lang"/>
    <xsl:apply-templates mode="h"/>
    </div>
    </xsl:template>

    <xsl:template mode="h">
    <xsl:element name="{name()}">
    <xsl:copy-of select="@"/>
    <xsl:apply-templates mode="h"/>
    </xsl:element>
    </xsl:template>

    that stops the namespace nodes being copied from the source
    and add

    exclude-result-prefixes = "myml"

    to your xsl:stylesheet to stop the myxml namespace nodes being copied
    from the stylesheet.

    David
     
    David Carlisle, Dec 14, 2004
    #11
  12. me: <xsl:template mode="h">

    should be

    <xsl:template mode="h" match="*">
    ^^^^^^^^
    of course, sorry.
     
    David Carlisle, Dec 14, 2004
    #12
  13. "Joris Gillis" <> writes:

    > > I have an unwanted "xmlns" attribute showing up in my otherwise-valid
    > > XHTML result document from an XSLT transformation.

    > Hi,
    >
    > it's actually quite simple:
    > just add the 'exclude-result-prefixes' attribute to the 'xsl:stylesheet' element:
    >
    > <xsl:stylesheet exclude-result-prefixes="myml"/>
    >
    > regards,



    that stops namespaces escaping from the stylesheet, but not from being
    copied from the source.

    David
     
    David Carlisle, Dec 14, 2004
    #13
  14. Bernd Fuhrmann

    Joris Gillis Guest

    >> just add the 'exclude-result-prefixes' attribute to the 'xsl:stylesheet' element
    >
    > that stops namespaces escaping from the stylesheet, but not from being
    > copied from the source.

    Hi,

    I'm not sure I understand that; it gives the desired output with me, but I'm probably not reading very well.. I have the nasty tendancy to skip just the essential parts of one's posting:)


    btw,
    <xsl:element name="{name()}">
    <xsl:copy-of select="@"/>
    <xsl:apply-templates mode="h"/>
    </xsl:element>

    couldn't that be written as this way?
    <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates mode="h"/>
    </xsl:copy>


    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Spread the wiki (http://en.wikipedia.org)
     
    Joris Gillis, Dec 14, 2004
    #14
  15. "Joris Gillis" <> writes:

    > >> just add the 'exclude-result-prefixes' attribute to the 'xsl:stylesheet' element

    > >
    > > that stops namespaces escaping from the stylesheet, but not from being
    > > copied from the source.

    > Hi,
    >
    > I'm not sure I understand that; it gives the desired output with me, but I'm probably not reading very well.. I have the nasty tendancy to skip just the essential parts of one's posting:)
    >
    >
    > btw,
    > <xsl:element name="{name()}">
    > <xsl:copy-of select="@"/>
    > <xsl:apply-templates mode="h"/>
    > </xsl:element>
    >
    > couldn't that be written as this way?
    > <xsl:copy>
    > <xsl:copy-of select="@*"/>
    > <xsl:apply-templates mode="h"/>
    > </xsl:copy>
    >


    no,xsl:copy, like xsl:copy-of copies namespace nodes as well as the
    element.

    David
     
    David Carlisle, Dec 14, 2004
    #15
  16. Bernd Fuhrmann

    Greg Guest

    >
    > Add
    >
    > exclude-result-prefixes="xhtml"
    >
    > as an attribute of xsl:stylesheet.


    Right. Given that it's the "myml" prefix I want to exclude from the
    result document, I've added . . .

    exclude-result-prefixes="myml"

    .. . . to the opening tag of my style sheet. See below:

    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:myml="http://www.mywords.com"
    xmlns="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="myml"
    >


    The effect seems to be that the "myml" prefix does get excluded from
    the result document, as a prefix. (I.e. it doesn't prefix anything in
    the result document.) But the "http://www.mywords.com" namespace gets
    added to the result document nonetheless (exactly as it is declared in
    the source document, tied to the "myml" prefix) in any content that is
    copied from the source document with the XSLT "copy-of".

    The result now looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title />
    </head>
    <body>
    <div xml:lang="fr">
    <p xmlns:myml="http://www.mywords.com">
    Tu es <em>belle</em>.
    </p>
    </div>
    </body>
    </html>

    Which, with a "p" element that has a "xmlns:myml" attribute, is still
    not valid XHTML 1.0 Strict.
    Hmmmmmm.

    Any further ideas on how I can make this valid?

    Thanks.
     
    Greg, Dec 16, 2004
    #16
  17. The effect seems to be that the "myml" prefix does get excluded from
    the result document, as a prefix. (I.e. it doesn't prefix anything in
    the result document.)


    No, exclude-result-prefixes has no effect on wheneter any elements are
    prefixed or not it stops any namespace node with namespace uRI
    http://www.mywords.com being copied from the stylesheet uless it is
    needed to generate an element or attribute name in that namespaace.

    But the "http://www.mywords.com" namespace gets
    added to the result document nonetheless

    In that case namespace nodes from that namespace must have been copied
    from the source tree (exclude-result-prefixes has _no_ effect on nodes
    copied from the source) see earlier recursove template posted which can
    be used to avoid copying namespace nodes.

    David
     
    David Carlisle, Dec 16, 2004
    #17
  18. In article <>,
    Greg <> wrote:
    >But the "http://www.mywords.com" namespace gets
    >added to the result document nonetheless (exactly as it is declared in
    >the source document, tied to the "myml" prefix) in any content that is
    >copied from the source document with the XSLT "copy-of".


    exclude-result-prefixes just stops namespace nodes being copied from
    literal result elements in the stylesheet. The rationale is that you
    may need to bind prefixes in the stylesheet just to address the source
    document, when they have no relevance to the output.

    But if you copy nodes from the source document that have namespace
    bindings in scope, it's up to you to remove any that you don't want.
    Since xsl:copy copies an element's namespace nodes, to get rid of them
    you will have to *not* use xsl:copy for elements. You can instead use
    a template that matches elements, and which creates an element with
    the same namespace-uri and local-name. You may also want to copy the
    namespace nodes except the myml: one, but you can probably get away
    without doing this (because if they are required for element or
    attribute names they will get inserted anyway).

    -- Richard
     
    Richard Tobin, Dec 16, 2004
    #18
  19. Bernd Fuhrmann

    Greg Guest

    I didn't see David's earlier post with the recursive template.

    In my style sheet, I tried . . .

    <xsl:template match="myml:translation">
    <div>
    <xsl:copy-of select="@xml:lang"/>
    <xsl:apply-templates mode="h"/>
    </div>
    </xsl:template>

    <xsl:template match="*" mode="h">
    <xsl:element name="{name()}">
    <xsl:copy-of select="@"/>
    <xsl:apply-templates mode="h"/>
    </xsl:element>
    </xsl:template>

    .. . . but Ant complained about the line:

    <xsl:copy-of select="@"/>

    Saying:

    [xslt] Unknown file:39:38: Fatal Error! A node test that matches either
    NCNam:* or QName was expected.

    So I replaced the "@" with "attribute::*", and no complaints - just an
    XHTML 1.0 Strict web page that is . . . valid! I have found peace and
    oneness with the world again. Thankyou.

    For the benefit of others find this thread and want to include XHTML in
    their own namespace-using XML document and then transform that XML
    document into valid XHTML . . .

    The source XML document:

    <?xml version="1.0" encoding="UTF-8"?>
    <myml:thought
    xmlns:myml="http://www.mywords.com"
    xmlns="http://www.w3.org/1999/xhtml"
    >


    <myml:translation xml:lang="fr">
    <p>
    Tu es <em title="Hot!">belle</em>.
    </p>
    </myml:translation>

    </myml:thought>


    The XSLT style sheet:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:myml="http://www.mywords.com"
    exclude-result-prefixes="myml"
    >


    <xsl:eek:utput
    method="xml"
    version="1.0"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"

    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    indent="yes"
    media-type="application/xhtml+xml"
    />

    <xsl:template match="myml:thought">
    <html>
    <head>
    <title>Thought</title>
    </head>
    <body>
    <xsl:apply-templates select="myml:translation"/>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="myml:translation">
    <div>
    <xsl:copy-of select="@xml:lang"/>
    <xsl:apply-templates mode="h"/>
    </div>
    </xsl:template>

    <xsl:template match="*" mode="h">
    <xsl:element name="{name()}">
    <xsl:copy-of select="attribute::*"/>
    <xsl:apply-templates mode="h"/>
    </xsl:element>
    </xsl:template>

    </xsl:stylesheet>


    And the resulting document, transformed with Ant 1.6.2's "xslt" task:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Thought</title>
    </head>
    <body>
    <div xml:lang="fr">
    <p>
    Tu es <em title="Hot!">belle</em>.
    </p>
    </div>
    </body>
    </html>

    Valid according to http://validator.w3.org/
    Thank you all. And thank you David for the recursive template.
     
    Greg, Dec 16, 2004
    #19
  20. David Carlisle, Dec 16, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    574
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,138
  3. Trygve

    namespace problem with XSLT

    Trygve, Feb 20, 2004, in forum: XML
    Replies:
    4
    Views:
    499
    Trygve
    Feb 20, 2004
  4. Replies:
    1
    Views:
    428
    David Carlisle
    Dec 15, 2004
  5. Anonymous
    Replies:
    3
    Views:
    534
    Ron Natalie
    Aug 18, 2003
Loading...

Share This Page