0

我有一个使用命名空间的 XSD 文档,简化为:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:xns="urn:MyNs" targetNamespace="urn:MyNs">
  <xs:complexType name="Type1">
    <xs:sequence>
      <xs:element name="xxx" type="xs:short" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="Type2">
    <xs:sequence>
      <xs:element name="yyy" type="xs:short" />
      <xs:element name="value" type="xns:Type1" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="Type3">
    <xs:sequence>
      <xs:element name="yyy" type="xs:short" />
      <xs:element name="value" type="xns:Type2" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

我需要处理该文件中的类型依赖关系,因为我有以下 XSL(再次简化为最小示例):

<?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">
    <xsl:output method="text" encoding="utf-8" indent="yes"/>

    <xsl:param name="debug" select="no"/>

    <xsl:strip-space elements="*"/>

    <xsl:key name="type" match="xs:complexType" use="@name"/>

    <xsl:template match="*" mode="print-dependency">
        <xsl:value-of select="@name"/>
        <xsl:text>&#x0a;</xsl:text>
    </xsl:template>

    <xsl:template match="*" mode="process-type">
        <xsl:apply-templates mode="print-dependency"
                select="key('type', (
                    xs:attribute | xs:complexContent/xs:extension//xs:attribute |
                    xs:sequence/xs:element | xs:complexContent/xs:extension/xs:sequence/xs:element
                )/@type)"/>
    </xsl:template>

    <xsl:template match="/xs:schema">
        <xsl:apply-templates select="xs:complexType" mode="process-type">
            <xsl:with-param name="debug" select="$debug"/>
        </xsl:apply-templates>
    </xsl:template>

</xsl:stylesheet>

如果我在输入 XSD 文件中没有命名空间(即,如果我删除“xns”),则输出如预期:

Type1
Type2

(Type1 显示为 Type 2 的依赖项,Type2 显示为 Type3 的依赖项)

但是,对于命名空间,输出为空,类型名称显然不匹配。即使像示例中那样使用命名空间,是否也可以管理它?(我不知道如何更改 key() 以匹配默认使用“targetNamespace”的 complexType,因此没有在“name”中明确指定它)

4

1 回答 1

0

好的,所以要根据@michael 的评论回答我自己的问题,我通过使用此 XSLT 使其工作 - 删除key()并使用显式匹配,而不是非剥离和剥离字符串:

<?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">
    <xsl:output method="text" encoding="utf-8" indent="yes"/>

    <xsl:param name="debug" select="no"/>

    <xsl:strip-space elements="*"/>

    <xsl:key name="type" match="xs:complexType" use="@name"/>

    <xsl:template match="*" mode="print-dependency">
        <xsl:value-of select="@name"/>
        <xsl:text>&#x0a;</xsl:text>
    </xsl:template>

    <xsl:template match="*" mode="process-dependency">
        <xsl:variable name="type" select="@type"/>
        <xsl:apply-templates mode="print-dependency"
                select="//xs:complexType[@name=$type]"/>
        <xsl:apply-templates mode="print-dependency"
                select="//xs:complexType[@name=substring-after($type, ':')]"/>
    </xsl:template>

    <xsl:template match="*" mode="process-type">
        <xsl:apply-templates mode="process-dependency"
                select="(
                    xs:attribute | xs:complexContent/xs:extension//xs:attribute |
                    xs:sequence/xs:element | xs:complexContent/xs:extension/xs:sequence/xs:element
                )"/>
    </xsl:template>

    <xsl:template match="/xs:schema">
        <xsl:apply-templates select="xs:complexType" mode="process-type">
            <xsl:with-param name="debug" select="$debug"/>
        </xsl:apply-templates>
    </xsl:template>

</xsl:stylesheet>

然后,这完全适用于我需要的命名空间和非命名空间类型名称(或它们的混合)。如果类型名称可以包括命名空间并且在没有命名空间的情况下被引用,则还可以选择添加两个额外的可能性。

于 2016-08-29T09:55:21.090 回答