cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Per Kreipke" <...@onclave.com>
Subject RE: [Q] exclude-result-prefixes...
Date Sat, 24 Aug 2002 20:37:45 GMT
Thanks to everyone for their pointers. I had also read Michael Kay's
comments at the indispensable XSLT FAQ here:

http://www.dpawson.co.uk/xsl/sect2/sect21.html

The solution below (from the discussion thread) is similar to the solution I
ended up with:

    <xsl:template match="*" priority="-1" mode="copy">
        <xsl:element name="{name()}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates mode="copy"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="text()" mode="copy">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>

The only thing is be in the habit of calling

<xsl:apply-templates mode="copy"/>

instead of <xsl:copy-of>, and adding a 'select=' if you want only portions
of the child tree.

Note 1: my only concern about the version above is the use of <xls;copy-of>
on the attributes. It's correct if attributes don't have 'unused' namespace
declarations on them such that attributes with a prefix have only that
prefix copied. But if attributes inherit the parent namespaces the way that
elements do, it's not correct.

My solution:

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

[I didn't use priority = -1]
[I actually strung each <xsl:template> on a single line to avoid extra
whitespace in results]



Note 2: both solutions above are better than the one below that was
mentioned in the thread and at

 http://outerthought.net/wiki/Wiki.jsp?page=RemoveNamespaces


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="*">
      <!-- remove element prefix (if any) -->
      <xsl:element name="{local-name()}">
        <!-- process attributes -->
        <xsl:for-each select="@*">
          <!-- remove attribute prefix (if any) -->
          <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
          </xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates/>
      </xsl:element>
  </xsl:template>
</xsl:stylesheet>

which only works for elements that contain only attributes and text. It
won't work for nested XML/elements.


The only other partial solution I've thought of is going ahead and declaring
all the namespaces on the root element of the doc in the result tree
fragment, thereby avoiding the often multiple declarations throughout the
XML below. What I get a lot is:

<root>
   ...
   <element>
       <dir:file xmlns:dir="urn:..." /> <!-- ns is generated -->
	 <dir:file xmlns:dir="urn:..." /> <!-- ns is generated -->
       ...
   </element>

And I think if I simply declare the namespace on the root, it'll only appear
there.

<root xmlns:dir="urn:..." >
   ...
   <element>
       <dir:file /> <!-- ns is generated -->
	 <dir:file /> <!-- ns is generated -->
       ...
   </element>

It's not gone, but the file is smaller in serialized form and quicker to
process perhaps?

Per


---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <cocoon-users-unsubscribe@xml.apache.org>
For additional commands, e-mail:   <cocoon-users-help@xml.apache.org>


Mime
View raw message