我有一种情况,我有一个充满 xsd 文件的目录,需要对它们进行转换,为它们中的每一个生成一个输出文件。我的样式表在单个文档上运行良好,但我想扩展它。好吧,目前我还没有使用 xslt 编辑器,已经安装了 saxon。这是xslt文件:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
<xsl:output method="text"/>
<!--* Ignore anything that looks complicated *-->
<xsl:template match="xsd:attribute
| xsd:attributeGroup
| xsd:group
| xsd:schema/xsd:element[@type]
| xsd:notation
| xsd:annotation
"/>
<!--* Ignore text nodes (otherwise the output will be
* inundated with whitespace) *-->
<xsl:template match="text()"/>
<!--* Top-level elements with local complex types; those
* we want to handle.
*-->
<xsl:template match = "xsd:schema/xsd:element[xsd:complexType]">
<xsl:apply-templates/>
</xsl:template>
<!--* Aha! A complex type whose content model we want to turn
* into a regular expression
*-->
<xsl:template match = "xsd:element/xsd:complexType
[xsd:sequence | xsd:choice | xsd:all]">
<!--* write out the name for the named regex *-->
<xsl:value-of select="concat('

',
@name, parent::xsd:element/@name,
' ')"/>
<!--* write out the regex *-->
<xsl:apply-templates/>
</xsl:template>
<!--* Simple recursive case: we encounter a model group. *-->
<xsl:template match = "xsd:sequence|xsd:choice|xsd:all">
<!--* Parenthesize the group and handle its children. *-->
<xsl:text>(</xsl:text>
<xsl:apply-templates/>
<xsl:text>)</xsl:text>
<!--* Append *, ?, +, or {min, max}. *-->
<xsl:call-template name="occurrence-indicator"/>
<!--* If our parent has further children,
* append the appropriate connector. *-->
<xsl:call-template name="connector"/>
</xsl:template>
<!--* An element in a content model. *-->
<xsl:template match = "xsd:element[ancestor::xsd:complexType]">
<!--* Write out the element's name. We're lazy so
* we don't bother with a QName for a local element.
* Also, we don't recur. *-->
<xsl:value-of select="concat(@ref, @name)"/>
<!--* Handle occurrence indicator and connect
* just as for groups. *-->
<xsl:call-template name="occurrence-indicator"/>
<xsl:call-template name="connector"/>
</xsl:template>
<!--* Emit the appropriate occurrence indicator for
* a group or element.
* Use {min,max}, {min,}, or {n} notation for
* non-standard occurrence counts.
*-->
<xsl:template name="occurrence-indicator">
<xsl:choose>
<xsl:when test="(@minOccurs='1' or not(@minOccurs))
and
(@maxOccurs='1' or not(@maxOccurs))">
<xsl:text></xsl:text>
</xsl:when>
<xsl:when test="@minOccurs='0'
and
(@maxOccurs='1' or not(@maxOccurs))">
<xsl:text>?</xsl:text>
</xsl:when>
<xsl:when test="@minOccurs='0' and @maxOccurs='unbounded'">
<xsl:text>*</xsl:text>
</xsl:when>
<xsl:when test="(@minOccurs='1' or not(@minOccurs))
and
@maxOccurs='unbounded'">
<xsl:text>+</xsl:text>
</xsl:when>
<xsl:when test="@minOccurs=@maxOccurs">
<xsl:value-of select="concat('{', @minOccurs,'}')"/>
</xsl:when>
<xsl:when test="@maxOccurs='unbounded'">
<xsl:value-of select="concat('{', @minOccurs,',}')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('{',
@minOccurs,
',',
@maxOccurs,
'}')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="connector">
<!--* Emit the appropriate connector, if we need one. *-->
<xsl:if test="following-sibling::*[self::xsd:sequence
| self::xsd:choice
| self::xsd:all
| self::xsd:element]">
<xsl:choose>
<xsl:when test="parent::xsd:sequence">
<xsl:text>, </xsl:text>
</xsl:when>
<xsl:when test="parent::xsd:choice">
<xsl:text> | </xsl:text>
</xsl:when>
<xsl:when test="parent::xsd:all">
<xsl:text> & </xsl:text>
</xsl:when>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>