1

I don't know much about xsl.

I have to compare 2 very large xmls which are practically the same, only that the attributes in their nodes are out of order and that is why the winmerge marks it as different

How would an xsl be that leaves everything the same and only orders the attributes in each node?

Currently I found this code on the internet, but it orders the entire document and I need it to be exactly the same as it is and only order the attributes alphabetically

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*">
                <xsl:sort select="name()" />
            </xsl:apply-templates>
            <xsl:apply-templates select="node()">
                <xsl:sort select="name()" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

winmerge example

<variable name="Invoke_Event_PCM_OP_AR_EVENT_ADJUSTMENT_OutputVariable" messageType="brm:PCM_OP_AR_EVENT_ADJUSTMENT_outmsg" xml:id="id_20" />
<variable messageType="brm:PCM_OP_AR_EVENT_ADJUSTMENT_outmsg" name="Invoke_Event_PCM_OP_AR_EVENT_ADJUSTMENT_OutputVariable" xml:id="id_20" />

For example, in the image winmerge detects differences, but the node is called the same, only that they have the attributes in a different order

I want to order only the attributes in alphabetical order so that winmerge detects that the lines are equal

  • For more help here you need to edit your post to include an example of what is correct and what is not, and explain a bit more about your issue. Which attributes are different? Is it many throughout the file, or just in a header? Your best bet is to use an XML library of some sort to open each file and order the attributes as required, you can find many libraries and tutorials using your favourite search engine. – sorifiend May 17 '22 at 00:10
  • Ahh, in that case see here https://stackoverflow.com/questions/16029523/manipulate-order-of-xml-element-attributes and here https://stackoverflow.com/questions/726395/order-of-xml-attributes-after-dom-processing Basically XML as a standard can never guarantee the order. You have two options, 1) Open both files with a Java scanner, or another line by line reader and order the attributes with simple text editing, or 2) parse the XML file with a library that can guarantee an attribute order (alpha numeric order maybe) and then save it back to the file, this should guarantee that both are the same – sorifiend May 17 '22 at 00:52
  • Or better yet, don't use winmerge, it only sees text, instead use another application that understands and works with XML. – sorifiend May 17 '22 at 00:53
  • "Currently I found this code on the internet, but it orders the entire document and I need it to be exactly the same as it is and only order the attributes alphabetically" I find this confusing. How to order something without changing it? – Just another Java programmer May 17 '22 at 01:12
  • I have been looking for some other application for a long time but they are all the same as winmerge, it detects the difference – Marcos Sandoval May 17 '22 at 01:27
  • 1
    FYI, the order of XML attributes is **by definition** insignificant - therefore the XSLT language does not provide a way to order them. You *may* be able to find a way to control the order by writing your stylesheet in a certain way - but that would be entirely dependent on your particular XSLT processor. – michael.hor257k May 17 '22 at 02:44
  • Instead of using XSLT to sort the attributes, consider using it to compare the two documents, using the [deep-equal](https://www.w3.org/TR/2010/REC-xpath-functions-20101214/#func-deep-equal) function.This will ignore the order of attributes and return `true` in your example. – michael.hor257k May 17 '22 at 03:41
  • Some processors provide additional serialization properties/methods, for instance Saxon (alas only in the commercial PE or EE but perhaps you use oXygen or similar where those are provided as the main XSLT engine anyway) has an attribute `` https://www.saxonica.com/html/documentation11/extensions/output-extras/serialization-parameters.html which (with some other normalizations) ensures that "Attributes within a start tag are sorted first by namespace URI, then by local name.". Perhaps that is an option. – Martin Honnen May 17 '22 at 10:16
  • thanks to all, but at last I found an application that does not mark the differences like winmerge, this application detect that they are the same regardless of the order of the node attributes – Marcos Sandoval May 17 '22 at 15:35

0 Answers0