tags:

views:

118

answers:

1

I have about 10 xml schemas that I have to clean up by alphabetizing the elements/types. I googled for a while and found someone's solution,

Here

which doesn't work for me for whatever reason (it skips from xsd:schema straight to xsd:comlplexType name=Account). Here is the simplist example, as you can see the first 10 or so elements are in order, but when the developer added 2 new fields they are out of order (crossRef and ID). I want to make sure that CrossRef and ID are alphabetized not only in the sequence when defined as part of the Account object, but also, if they were defined as their own types separately (these two fields aren't but the rest are) that their definitions would also be alphabetized with the rest of the definitions.

<?xml version="1.0" encoding="utf-8"?><xsd:schema targetNamespace="####" xmlns="####" xmlns:lms.base="####/base" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
<xsd:import namespace="###/base" schemaLocation="Base.xsd" />

<xsd:element name="Account" type="Account" />

<xsd:complexType name="Account" >

    <xsd:complexContent >
        <xsd:extension base="lms.base:ExtensionBase">
            <xsd:sequence >
                <xsd:element name="Address" type="lms.base:Address" minOccurs="0" maxOccurs ="unbounded" />       
                <xsd:element name="BusinessSegment" type="lms.base:BusinessSegment" minOccurs ="0" maxOccurs ="1" />
                <xsd:element name="Name" type="AccountName" minOccurs ="0" maxOccurs ="1" />
                <xsd:element name="Number" type="AccountNumber" minOccurs ="0" maxOccurs ="1" />
                <xsd:element name="Office" type="AccountOffice" minOccurs ="0" maxOccurs ="1" />
                <xsd:element name="Status" type="AccountStatus" minOccurs ="0" maxOccurs ="1" />
                <xsd:element name="UWMemo" type="lms.base:UWMemo" minOccurs ="0" maxOccurs ="1" />
      <xsd:element name="CrossRef" type="lms.base:string-minLength" minOccurs="0" maxOccurs="unbounded" />
            </xsd:sequence>
    <xsd:attribute name="ID" type="xsd:long" />
        </xsd:extension>
    </xsd:complexContent>       
</xsd:complexType>

<xsd:simpleType name="AccountID">
    <xsd:restriction base="lms.base:ID" />
</xsd:simpleType>

<xsd:simpleType name="AccountName">
    <xsd:restriction base="lms.base:string-minLength" />
</xsd:simpleType>

<xsd:simpleType name="AccountNumber">
    <xsd:restriction base="lms.base:string-minLength" />
</xsd:simpleType>

<xsd:simpleType name="AccountStatus">
    <xsd:restriction base="lms.base:string-minLength">
        <xsd:enumeration value="Not_Assigned" />
        <xsd:enumeration value="Active" />
        <xsd:enumeration value="Inactive" />
        <xsd:enumeration value="Targeted/Prospect" />
        <xsd:enumeration value="Lost" />
        <xsd:enumeration value="Submission Declined" />
        <xsd:enumeration value="Terminated" />
        <xsd:enumeration value="Claim" />
        <xsd:enumeration value="Watch List" />
        <xsd:enumeration value="H.O. Hold/Suspend" />
        <xsd:enumeration value="Infrequent" />
    </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="AccountOffice">
    <xsd:complexContent>
        <xsd:extension base="lms.base:Value"/>
    </xsd:complexContent>
</xsd:complexType>

+1  A: 

You want to sort everything by name attribute, right? In that case, this might work:

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'&gt;

    <!-- Sort child elements by name, if any child has one -->
    <xsl:template match="*[*/@name]">
        <xsl:copy>
            <xsl:copy-of select="@*|text()"/>
            <xsl:apply-templates select="*">
                <xsl:sort select="@name"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <!-- Identity template for everything else -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*">
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
markusk
You are a genius. That works perfectly. May I buy you a beer?
Sublimemm
@Sublimemm: Happy to help. :-)
markusk