Attribute order in Xerces DOM to XML

We are building a DOM in memory, using Xerces C++, which we then dump out to an xml file using a simple copy-of xslt file. The problem is that the attribute order in the output is not the same as the order that they are assigned to the DOM, they come out in alphabetical order. Now, does anyone have a way of making the output stick to the order of input for attributes, in order to save me writing a largish stylesheet just to swap a couple of attributes around?
NogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

savalouCommented:
Why do you care what the attribute order is?
0
NogAuthor Commented:
Because we are posting the output to an external client who will validate it with a DTD and they require thet the attributes are in that order.
0
savalouCommented:
But attribute order doesn't matter to a validating parser.  It's only people who look at it who might care.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

NogAuthor Commented:
Regardless, we are constrained to provide the XML as specified.
0
savalouCommented:
Ok, well, here we go.
What do you use as your DOM Parser?  What do you use for doing the XSL transform?  What does your XSL file look like?  I used Xerces-C 2.3/Xalan-C 1.6 on the following foo.xml and foo.xsl files and I got the attributes out in their original order.  I also got the same results with Xerces-J 2.5.0 and Xalan-J 2.5.2.

<!-- foo.xml -->
<?xml version="1.0" encoding="utf-8"?>
<root>
<element>
 <item><title beta="1" alpha="1" rho="9">Temp</title></item>
 <item><title>Time</title></item>
</element>
<element>
 <item><title a="1" z="2" c="3">Temp</title></item>
 <item><title>Time</title></item>
</element>
</root>

<!-- foo.xsl -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rdcproCommented:
It's a ridiculous, and unenforceable, constraint.  

It is simply not possible to constrain attribute order in a DTD or schema.  There are no guarantees that the attributes are in any order after parsing.   The actual serialized attribute order will always be implementation dependant, and it depends on the serializer not the DOM, and it's possible that it could change with successive releases of a given implementation's parser/serializer.  Attributes are properties of a node, not children, so they don't have an implicit concept of order except where a document is serialized.

If you want to change the order of attributes _as serialized_, savalou is on the right track, but you'll have to add a boatload of rules that identify what the order for each attribute should be.  That is:

<?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" encoding="UTF-16" indent="yes"/>
<!-- don't really want to match elements here! -->
<xsl:template match="/ | node()">
      <xsl:copy>
            <xsl:apply-templates select="node()"/>
      </xsl:copy>
</xsl:template>

<xsl:template match="*">
    <!-- Constrain the attribute order output...but no guarantee this will have any effect any time the XML is later parsed -->
    <xsl:apply-templates select="@foo|@bar|@snafu|@tarfu|@fubar"/>
    <xsl:apply-templates select="node()"/>
</xsl:template>

<xsl:template match="@*">
      <xsl:copy>
      </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Regards,
Mike Sharp
0
savalouCommented:
>savalou is on the right track,

Hey, hey, hey, I'm not on any track here.  
0
savalouCommented:
Nog you need to figure out where the sorted attributes are coming from.   I think it is entirely possible that it's the DOM parser because after all, if you say getAttributes() and it returns an array of attributes, isn't that the DOM?  

So test out the foo stuff and see if you get the attributes out in the same order or if they're sorted.  As you said, you don't want to jam up your XSL with a bunch of attribute selection productions.  

0
rdcproCommented:
I think the problem is that they're creating the XML in DOM, but when they serialize it to send it on, the attribute order is different.  Sorting an array of attributes wouldn't help here, I think.  

Regards,
Mike Sharp
0
NogAuthor Commented:
Yes, the problem is definately that we are creating the XML in DOM and then serializing, then the order is different. I totally agree that the condition we have had put on us is "unfortunate" to use political parlance. Still, we have to go with what we are given. I've been working on a style sheet to do the job, looks like I may have to go that way.
Thanks for trying guys.
0
rdcproCommented:
Without changing parsers, and unless there's an implementation dependant way of changing the default order in Xerces, I think your only approach is to use the XSLT.   MSXML, I believe, serializes in document order, though I've never actually tried it with attributes.  After all, there is no insertBefore() method with attributes like there is with elements.

Regards,
Mike Sharp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.