Solved

Attribute order in Xerces DOM to XML

Posted on 2003-11-07
13
2,342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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 dynamically set the form action using jQuery.

728 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