tags:

views:

43

answers:

1

I have a situation where particular elements (xs:simpleType, xs:complexType) are placed in the output as they are encountered:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:complexType name="HighSchoolType">
 <xs:sequence>
  <xs:element name="OrganizationName" type="core:OrganizationNameType"/>
  <xs:element name="OPEID" type="core:OPEIDType"/>
  <xs:simpleType name="OPEIDType"/>
 </xs:sequence>
  </xs:complexType>
  <xs:simpleType name="OrganizationNameType"/>
</xs:schema>

I'd prefer that the xs:simpleType always be attached to the root, no matter where the pattern is encountered in the source. IE:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:complexType name="HighSchoolType">
 <xs:sequence>
  <xs:element name="OrganizationName" type="core:OrganizationNameType"/>
  <xs:element name="OPEID" type="core:OPEIDType"/>
 </xs:sequence>
  </xs:complexType>
  <xs:simpleType name="OrganizationNameType"/>
  <xs:simpleType name="OPEIDType"/>
</xs:schema>

Is it possible to stop duplicates at this point as well?

Here's the template I'm currently using:

<xsl:template match="xs:simpleType">
 <xsl:copy>
  <xsl:copy-of select="*[not(self::xs:annotation or self::xs:restriction)]|@*"/>
 </xsl:copy>
</xsl:template>
+1  A: 

Something like this should work:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    <xsl:template match="/xs:schema">
     <xsl:copy>
      <xsl:apply-templates/>
      <xsl:copy-of select="//xs:simpleType"/>
     </xsl:copy>
    </xsl:template>

    <xsl:template match="*[name()!='xs:simpleType' and name()!='xs:schema']">
     <xsl:copy>
      <xsl:apply-templates select="*|@*"/>
     </xsl:copy>
    </xsl:template>

    <xsl:template match="@*">
     <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>
steamer25
steamer25 - that works, thanks. It looks like I'll have to run two xsl files to get my desired output.
OMG Ponies