Search code examples
c#xmlxsd

Remove elements/attributes of XML by XSD


I want to remove all elements and/or attributes of a .xml-file that not match definitions of a .xsd-file.

I provide simplified version of a XML-XSD-filepair, but they actually have more attributes. So creating XSL transformation completely manually would be seamless work.

ItemProduceRecipe.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            elementFormDefault="qualified">
    <xsd:complexType name="ItemProduceRecipe">
        <xsd:sequence>
            <xsd:element name="Recipe" type="ItemProduceRecipe_Recipe" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="ItemProduceRecipe_Recipe"
                     dc:keys="id">
        <xsd:sequence>
            <xsd:element name="Materials" type="ItemProduceRecipe_Recipe_Materials" />
            <xsd:element name="Result" type="ItemProduceRecipe_Recipe_Result" />
        </xsd:sequence>

        <xsd:attribute name="id" type="xsd:int" use="required" />
        <xsd:attribute name="needGrade" type="xsd:int" use="required" />
        <xsd:attribute name="needSkillProf" type="xsd:int" use="required" />
    </xsd:complexType>

    <xsd:complexType name="ItemProduceRecipe_Recipe_Materials">
        <xsd:sequence>
            <xsd:element name="Material" type="ItemProduceRecipe_Recipe_Materials_Material" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="ItemProduceRecipe_Recipe_Materials_Material">
        <xsd:attribute name="count" type="xsd:int" use="required" />
        <xsd:attribute name="id" type="xsd:int" use="required" />
    </xsd:complexType>

    <xsd:complexType name="ItemProduceRecipe_Recipe_Result">
        <xsd:attribute name="count" type="xsd:int" use="required" />
        <xsd:attribute name="id" type="xsd:int" use="required" />
    </xsd:complexType>

    <xsd:element name="ItemProduceRecipe" type="ItemProduceRecipe" />
</xsd:schema>

ItemProduceRecipe.xml:

<ItemProduceRecipe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Recipe id="1466" needGrade="0" needSkillProf="0" junkA="cc">
        <Materials>
            <Material id="206817" count="1"/>
        </Materials>
        <Result id="200999" count="2"/>
    </Recipe>
    <Recipe id="28528" needGrade="1" needSkillProf="500" junkB="dd">
        <Materials>
            <Material id="206817" count="15"/>
            <Material id="206818" count="45"/>
            <Material id="204050" count="300"/>
        </Materials>
        <Result id="116" count="2"/>
    </Recipe>
</ItemProduceRecipe>

Expected Output: Should remove junkA and junkB attribute.

Other similar threads:

Xml Cleaning based on XSD in C#

  • Unfortunately this needs a lot manual work and only includes elements not attributes.

Generating an XSLT file from XSD based upon a 1:1 mapping XSD compliant XML in -> XSD compliant XML out

1. Is creating XSLT via XSD the recommended approach and do I need to adapt anything for my files?

2. Would it be recommended to load XSD by XSL transformation and remove all not matching, if yes how would this be done by C#?


Solution

  • What worked for me so far was to store element and attribute names of .xsd in Dictionaries and check them in .xml-file.