Solved

Attribute order in Xerces DOM to XML

Posted on 2003-11-07
13
2,237 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
Comment Utility
Why do you care what the attribute order is?
0
 

Author Comment

by:Nog
Comment Utility
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
Comment Utility
But attribute order doesn't matter to a validating parser.  It's only people who look at it who might care.
0
 

Author Comment

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

Accepted Solution

by:
savalou earned 125 total points
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 26

Assisted Solution

by:rdcpro
rdcpro earned 125 total points
Comment Utility
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
Comment Utility
>savalou is on the right track,

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

Expert Comment

by:savalou
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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, …
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now