0

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

Community
  • 1
  • 1
Lanre
  • 61
  • 4
  • 1
    It is unclear what you're asking and the question is also too broad. Please specify what you want to achieve and what you have tried so far. If possible include an example. You can probably get sufficient inspiration for that from the question you linked to. – lord.garbage Oct 14 '14 at 14:43
  • I need an XSLT that does the reverse of that in the document linked to, i.e. Venetian Blind to Russian Doll instead of Russian Doll to Venetian Blind. – Lanre Oct 14 '14 at 16:29
  • 1
    StackOverflow is neither a code writing service nor tool/library recommendation service, so you'll have to try writing the code and asking for specific help or your question will likely be closed. – kjhughes Oct 14 '14 at 16:47
  • Apologies for the error - I am new to posting on Stack Overflow. I had hoped to send this to the chap that posted the question I linked to, but it appeared as a public question. – Lanre Oct 15 '14 at 06:47
  • 1
    While the question was poorly asked, in a sense, it is still a clearly understood question and one that I find interesting as well. The linked question gives an example of what constitutes a 'Russian Doll' style XSD and what constitutes a 'Venetian Blind' XSD. It's fair to infer that the linked question should serve as an adequate example, but also that this question is distinct. – Jefftopia Oct 16 '14 at 14:13
  • @Lanre I posted a question similar to yours recently. The response may be helpful for you too. http://stackoverflow.com/questions/26414092/xslt-copy-all-but-cut-and-paste-some-content/26414232#26414232 – Jefftopia Oct 17 '14 at 14:54
  • 1
    @Jefftopia thanks for the suggestion; actually, you've just given me an idea for the last step of the Venetian Blind to Russian Doll conversion, so that I end up with an element rather than a fully denormalised complex type. Thanks! – Lanre Oct 18 '14 at 00:20

1 Answers1

2

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>
Lanre
  • 61
  • 4