Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Attribute order in Xerces DOM to XML

Posted on 2003-11-07
13
Medium Priority
?
2,411 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

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

Accepted Solution

by:
savalou earned 500 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 500 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 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