0

I have a XML file that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:stuff>
        <xs:nestedStuff name="nested"/>
    </xs:stuff>
    <xs:simpleType name="A_B">
        <xs:union memberTypes="xs:A xs:B"/>
    </xs:simpleType>
    <xs:simpleType name="A_B_C">
        <xs:union memberTypes="xs:A xs:B xs:C"/>
    </xs:simpleType>
    <xs:simpleType name="A_B">
        <xs:union memberTypes="xs:A xs:B"/>
    </xs:simpleType>
    <xs:simpleType name="A_B">
        <xs:union memberTypes="xs:A xs:B"/>
    </xs:simpleType>
    <xs:simpleType name="A_B_C">
        <xs:union memberTypes="xs:A xs:B xs:C"/>
    </xs:simpleType>
    <xs:simpleType name="A_C">
        <xs:union memberTypes="xs:A xs:C"/>
    </xs:simpleType>
</xs:schema>

What I want to do is copy every single line of this file except for the duplicate simpleType elements, so that I would get

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:stuff>
        <xs:nestedStuff name="nested"/>
    </xs:stuff>
    <xs:simpleType name="A_B">
        <xs:union memberTypes="xs:A xs:B"/>
    </xs:simpleType>
    <xs:simpleType name="A_B_C">
        <xs:union memberTypes="xs:A xs:B xs:C"/>
    </xs:simpleType>
    <xs:simpleType name="A_C">
        <xs:union memberTypes="xs:A xs:C"/>
    </xs:simpleType>
</xs:schema>

I cannot use XLST-2.0. I am very new to XSLT, and I have tried various things from here, but it does not apply to my problem the way I thought it did. Here is an example of what I tried, and that results in an output exactly equal to the input:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:for-each select="simpleType/@name[not(.=preceding::*)]">
            <xsl:element name="xs:TEST">
            </xsl:element>
        </xsl:for-each> 

        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>

    </xsl:template>
</xsl:stylesheet>
Community
  • 1
  • 1
Efferalgan
  • 1,681
  • 1
  • 14
  • 24

1 Answers1

1

I. valid XML

Your shown xml is not wellformed and valid. You have to close the tag xs:nestedStuff. Not that important, because i believe it's a lack of minified example. Just saying.

II: 1.0 XSLT

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xsl:key match="xs:simpleType" use="@name" name="key-for-types"/>

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

    <xsl:template match="xs:simpleType[count(. | key('key-for-types', @name)[1]) &gt; 1]"/>

</xsl:stylesheet>

Identity-copy template to copy all nodes. YES it is possible to write this in a form of xsl:copy-of ..., but i prefer it that way on SO. Evaluate yourself, what way you are going for.

Remove all duplicate key-entries via Muenchian Grouping.

uL1
  • 2,117
  • 2
  • 17
  • 28
  • Right, I closed `nestedStuff`. Thanks a lot, this code works, I'll now have a look at the inner workings of Muenchian Grouping. – Efferalgan Oct 24 '16 at 11:01