xsl-list
[Top] [All Lists]

Re: [xsl] recursive sorting by element name -> Xalan Issue

2007-11-29 11:16:37
Hi Michael, thanks for clearing that up.  I am *almost* there now,
with the current incarnation of the stylesheet (below).

The only remaining issue I have appears to be with Xalan.  When I
serialize out a collection with JAXB, and apply the stylesheet (using
code posted previously), it sorts everything and I have no missing
attributes, except the DictionaryModelDescriptor node is now at the
bottom of the file instead of the top.  When I run with xsltproc on
mac/linux it places it at the top.

Any idea why I get this behavior?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:xalan="http://xml.apache.org/xslt"; >

    <!-- The xalan param gets back indentation that seems to be broken
in the java api  -->
    <xsl:output method="xml" indent="yes" encoding="UTF-8"
xalan:indent-amount="4"/>
    <xsl:strip-space elements="*" />

        <xsl:template match="/">
                <xsl:apply-templates />
        </xsl:template>

        <xsl:template match="@*|node()">
                <xsl:copy>
                        <xsl:copy-of select="@*"/>
                        <!-- copy all attributes before applying templates to 
children only -->
                        <xsl:apply-templates select="node()">
                                <xsl:sort select="- 
boolean(self::DictionaryModelDescriptor)"/>
                                <xsl:sort select="@typeName"/>
                                <xsl:sort select="name(.)"/>
                                <xsl:sort />
                        </xsl:apply-templates>
                </xsl:copy>
        </xsl:template>

</xsl:stylesheet>

On Nov 29, 2007 12:46 PM, Michael Kay <mike(_at_)saxonica(_dot_)com> wrote:

I am running into some issues / inconsistencies running this
transformation on command line vs. within java vs. which
platform I run
it on.  I'm hoping the list might have some pointers on how
to resolve his.

        <xsl:template match="@*|node()">
                <xsl:copy>
                        <xsl:apply-templates select="@*|node()">
                                <xsl:sort select="@typeName"/>
                                <xsl:sort select="name(.)"/>
                                <xsl:sort />
                        </xsl:apply-templates>
                </xsl:copy>
        </xsl:template>

This code may have the effect of sorting child elements before attributes
(specifically a child element with no typeName attribute whose name
alphabetically precedes the attribute names). You aren't allowed to create
attributes for an element after creating child elements. In XSLT 1.0 the
processor has the option of ignoring the error by discarding the offending
attributes.

When I run this on Ubuntu Linux 7.10 (fully updated) using
xsltproc, I
encounter this bug:

https://bugs.launchpad.net/ubuntu/+source/libxml2/+bug/147144

Namely, it reports the error:

runtime error: file SortCollections.xsl line 40 element copy
Attribute
nodes must be added before any child nodes to an element.

That doesn't look like a bug to me, it looks like correct behaviour.


When I run this on Mac OS X 1.5 (Leopard -- fully updated) using
xsltproc, it does exactly what I want with no problems.

That looks like a bug to me.


When I run this script from Java 1.6  (using JAXB) with the
code below,
the identity transform does not copy all the attributes over.
I end up with missing attributes, and I have no idea why.

Xalan is apparently choosing the option to ignore the error and discard the
attributes.

Michael Kay
http://www.saxonica.com/



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





-- 
Zeno Consulting, Inc.
http://www.zenoconsulting.biz
248.894.4922 phone
313.884.2977 fax

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--