Re: [xslt] indentation problem and some related questions



Hi,

On 16/11/2008, Viktor Štujber <theultramage gmail com> wrote:
> Here's an example:
>
> XML document:
> <?xml version="1.0" encoding="UTF-8"?>
> <my:entry xmlns:my="http://mynamespace.org/";
>           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>           xsi:schemaLocation="test.xsd">
> <my:content xmlns="http://www.w3.org/1999/xhtml";>
> <h1>example data</h1>
> <p>hello there</p>
> <p>and one more</p>
> </my:content>
> </my:entry>
>
> When I deep-copy the insides of this thing's <content> element,  I get
> something like this:
> <h1 xmlns:my="http://mynamespace.org/";
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>example
> data</h1>
> <p xmlns:my="http://mynamespace.org/";
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>hello there</p>
> <p xmlns:my="http://mynamespace.org/";
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>and one more</p>

This is certainly not what you want!

> From what I believe, the xhtml elements inside content are in the
> default (xhtml) namespace, and not in the my: and neither in the xsi:
> namespace.
Correct.

> Also, the xmlns:* 'attributes' are just hints and are in no
> way related to the actual namespace the elements are in.
These are namespace declarations: bind a namespace-prefix to a namespace-URI.

> The element name prefix does that.
Yes.

I would like to propose 2 stylesheets. First if you would like to use copy-of.
It moves the namespace declarations to the outermost element and works
for both xsltproc and Saxon-6:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
   xmlns:my="http://mynamespace.org/";
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
   <body xmlns="http://www.w3.org/1999/xhtml";>
      <div>
         <xsl:copy-of select="my:entry/my:content/*"/>
      </div>
   </body>
</xsl:template>
</xsl:stylesheet>

The other solution uses the identity template and exclude-result-prefixes.
When used with xsltproc it gets rid of the namespace declarations, but
not with Saxon-6:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
   xmlns:my="http://mynamespace.org/";
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
   exclude-result-prefixes="my xsi"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
   <body xmlns="http://www.w3.org/1999/xhtml";>
      <div>
         <xsl:apply-templates select="my:entry/my:content/*"/>
      </div>
   </body>
</xsl:template>
<xsl:template match="node() | @*">
   <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
   </xsl:copy>
</xsl:template>
</xsl:stylesheet>

Hope this helps,

Manfred


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]