XSLT使用minOccurs标签将XSD转换为另一个XSD

时间:2013-10-14 13:24:06

标签: xslt xsd

我有一个现有的XSD,其中定义了一系列复杂类型。我需要为每个类型构建一个相关的XSD,但每个复杂类型中的每个元素都需要是可选的(minOccurs =“0”)。

我发现此帖子作为示例开始......但我无法添加可选标签。 transforming with xsl an xml schema template to an other xml schema template

任何比我更熟悉XSLT的人的帮助?

1 个答案:

答案 0 :(得分:2)

我没有测试过很多,因为你没有提供任何样本,但是基于Identity transform的样式表可以找到工作。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!-- Identity transform - copy everything into output -->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
    </xsl:template>

    <!-- for not-root xs:element ... -->
    <xsl:template match="xs:element[ancestor::xs:complexType]">
        <xsl:copy>
            <!-- ... add minOccurs element ...-->
            <xsl:attribute name="minOccurs">0</xsl:attribute>
            <!-- ... and copy everything but not minOccurs attribute if any -->
            <xsl:apply-templates select="node() | @*[not(name() = 'minOccurs')]" />     
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
相关问题