Solved

Attribute order in Xerces DOM to XML

Posted on 2003-11-07
13
2,271 Views
Last Modified: 2013-11-19
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?
0
Comment
Question by:Nog
  • 5
  • 3
  • 3
13 Comments
 
LVL 3

Expert Comment

by:savalou
ID: 9701643
Why do you care what the attribute order is?
0
 

Author Comment

by:Nog
ID: 9701683
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
 
LVL 3

Expert Comment

by:savalou
ID: 9701891
But attribute order doesn't matter to a validating parser.  It's only people who look at it who might care.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Nog
ID: 9702013
Regardless, we are constrained to provide the XML as specified.
0
 
LVL 3

Accepted Solution

by:
savalou earned 125 total points
ID: 9703241
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
 
LVL 26

Assisted Solution

by:rdcpro
rdcpro earned 125 total points
ID: 9703627
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
 
LVL 3

Expert Comment

by:savalou
ID: 9705274
>savalou is on the right track,

Hey, hey, hey, I'm not on any track here.  
0
 
LVL 3

Expert Comment

by:savalou
ID: 9705327
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
 
LVL 26

Expert Comment

by:rdcpro
ID: 9705349
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
 

Author Comment

by:Nog
ID: 9712963
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
 
LVL 26

Expert Comment

by:rdcpro
ID: 9716924
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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question