Home > Enterprise >  how to omit XSD schema tag 'sequence' with XSLT?
how to omit XSD schema tag 'sequence' with XSLT?

Time:02-19

  1. I have applied some XSLT formatter for our XML files (XML feeds), kind of prettifying , tags are in alphabetical order:
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

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

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

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

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

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

</xsl:stylesheet>

2.But few of ours XML files originally using specific XSD-schema which applying tag <xs:sequence> which demands strict sequence of the specified elements and schema validator fails.

Of course we are able to exclude this few files from prettifying, but question is : can we configure our XSLT in some way to avoid excluding these files? XSD-schema cannot be changed.

CodePudding user response:

You can of course try to use predicates like /*/*/*/*[not(self::xs:sequence)] in your match patterns, declaring xmlns:xs="http://www.w3.org/2001/XMLSchema". I won't try to spell out which patterns need that but I don't think an XSD schema can have an xs:sequence as /* or /*/*.

CodePudding user response:

Firstly, you don't need to have five identical template rules with different match patterns. A single template rule with match="*" will do the job just fine.

Secondly, order of elements is often significant whether or not there is a schema. Sometimes there is semantic significance (the order of paragraphs in a chapter needs to be retained, as does the order of authors of an article), sometimes there is a conventional order other than alphabetical (a table heading precedes the table body); sometimes receiving applications are written to expect a particular order. So I would suggest only applying your transformation to those rare documents where order of elements doesn't matter.

  • Related