XSL Venetian Blind

2019-08-31 00:19发布

问题:

Could anyone help me with an XSLT to convert an XSD from Venetian Blind to Russian Doll design? I read an article on Stack Overflow about the reverse: Russian doll to Venetian blind xsl transformation

回答1:

With a lot of help from a colleague, I finally got an answer. It may not be elegant, but it meets my immediate needs, here it is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    exclude-result-prefixes='exsl'
    version="2.0"
    xmlns:com="http://canaldigital.com/tsi/XSD/V5.00"
    xmlns:exsl="http://exslt.org/common"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="xsd:complexType">
       <xsl:copy>
           <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
       </xsl:copy>
    </xsl:template>

    <xsl:template name="complexTypeElemEmbedded">
        <xsl:param name="typeName"></xsl:param>
        <xsl:variable name="typeNameNoNS" select="substring-after($typeName, ':')" />
        <xsd:complexType>
            <xsl:apply-templates select="//xsd:complexType[@name=$typeNameNoNS]/node()"></xsl:apply-templates>
        </xsd:complexType>
    </xsl:template>

    <xsl:template match="xsd:element[@type]">
        <xsl:choose>
            <!-- All simple types have a name ending in the literal 'Type' -->
            <xsl:when test="(ends-with(@type, 'Type'))">
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:when>
            <!-- this picks up the global complex types -->
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:copy-of select="@*[(name()!='type')]"/>
                    <xsl:call-template name="complexTypeElemEmbedded">
                        <xsl:with-param name="typeName" select="@type"/>
                    </xsl:call-template>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

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

</xsl:stylesheet>


标签: xslt xsd