tags:

views:

625

answers:

1

I have the following:

<xs:schema>
<xs:complexType name="NameType">
 <xs:sequence>
  <xs:element name="NamePrefix" type="core:NamePrefixType" minOccurs="0"/>
  <xs:element name="FirstName" type="core:FirstNameType" minOccurs="0"/>
  <xs:element name="MiddleName" type="core:MiddleNameType" minOccurs="0" maxOccurs="10"/>
  <xs:element name="LastName" type="core:LastNameType" minOccurs="0"/>
  <xs:element name="NameSuffix" type="core:NameSuffixType" minOccurs="0"/>
  <xs:element name="NameTitle" type="core:NameTitleType" minOccurs="0"/>
  <xs:element name="CompositeName" type="core:CompositeNameType" minOccurs="0"/>
  <xs:element name="NoteMessage" type="core:NoteMessageType" minOccurs="0" maxOccurs="unbounded"/>
 </xs:sequence>
</xs:complexType>
<xs:simpleType name="NamePrefixType"/>
<xs:simpleType name="FirstNameType"/>
<xs:simpleType name="MiddleNameType"/>
<xs:simpleType name="LastNameType"/>
<xs:simpleType name="NameSuffixType"/>
<xs:simpleType name="NameTitleType"/>
<xs:simpleType name="CompositeNameType"/>
<xs:simpleType name="NoteMessageType"/>
<xs:complexType name="NameType">
 <xs:sequence>
  <xs:element name="NamePrefix" type="core:NamePrefixType" minOccurs="0"/>
  <xs:element name="FirstName" type="core:FirstNameType" minOccurs="0"/>
  <xs:element name="MiddleName" type="core:MiddleNameType" minOccurs="0" maxOccurs="10"/>
  <xs:element name="LastName" type="core:LastNameType" minOccurs="0"/>
  <xs:element name="NameSuffix" type="core:NameSuffixType" minOccurs="0"/>
  <xs:element name="NameTitle" type="core:NameTitleType" minOccurs="0"/>
  <xs:element name="CompositeName" type="core:CompositeNameType" minOccurs="0"/>
  <xs:element name="NoteMessage" type="core:NoteMessageType" minOccurs="0" maxOccurs="unbounded"/>
 </xs:sequence>
</xs:complexType>
<xs:simpleType name="NamePrefixType"/>
<xs:simpleType name="FirstNameType"/>
<xs:simpleType name="MiddleNameType"/>
<xs:simpleType name="LastNameType"/>
<xs:simpleType name="NameSuffixType"/>
<xs:simpleType name="NameTitleType"/>
<xs:simpleType name="CompositeNameType"/>
<xs:simpleType name="NoteMessageType"/>

How can I integrate duplicate node handling based on the following xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="core AcRec">

 <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>

Expected output:

<xs:schema>
  <xs:complexType name="NameType">
    <xs:sequence>
      <xs:element name="NamePrefix" type="core:NamePrefixType" minOccurs="0"/>
      <xs:element name="FirstName" type="core:FirstNameType" minOccurs="0"/>
      <xs:element name="MiddleName" type="core:MiddleNameType" minOccurs="0" maxOccurs="10"/>
      <xs:element name="LastName" type="core:LastNameType" minOccurs="0"/>
      <xs:element name="NameSuffix" type="core:NameSuffixType" minOccurs="0"/>
      <xs:element name="NameTitle" type="core:NameTitleType" minOccurs="0"/>
      <xs:element name="CompositeName" type="core:CompositeNameType" minOccurs="0"/>
      <xs:element name="NoteMessage" type="core:NoteMessageType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <xs:simpleType name="NamePrefixType"/>
  <xs:simpleType name="FirstNameType"/>
  <xs:simpleType name="MiddleNameType"/>
  <xs:simpleType name="LastNameType"/>
  <xs:simpleType name="NameSuffixType"/>
  <xs:simpleType name="NameTitleType"/>
  <xs:simpleType name="CompositeNameType"/>
  <xs:simpleType name="NoteMessageType"/>
</xs:schema>
+1  A: 

This copies the first distinct simpleType of any name but is a bit more explicit with the 'NameType' complexType:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="core AcRec"
    xmlns:core="foo" xmlns:AcRec="bar">

    <xsl:key name="simpleTypes" match="xs:simpleType" use="@name"/>

    <xsl:template match="/xs:schema">
     <xsl:copy>
      <xsl:copy-of select="xs:complexType[@name='NameType'][1]"/>
      <xsl:apply-templates />
      <xsl:copy-of select="//xs:simpleType[generate-id(.) = generate-id(key('simpleTypes', @name)[1])]" />
     </xsl:copy>
    </xsl:template>

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

    <xsl:template match="@*">
     <xsl:copy-of select="." />
    </xsl:template>

    <xsl:template match="*"/>
</xsl:stylesheet>

Note that the default template is overridden so that you don't get the undesired children of 'NameType'.

steamer25
...The same pattern with the key will work if you want to omit duplicates of any complexType with a matching name. If you need to do deep comparisons (i.e., the children of the complexType match as well as the name), that will probably take further consideration.
steamer25
No deep comparison needed, just needed to tweak so it omitted duplicate complexType with matching names.
OMG Ponies