将xsl和xml架构模板转换为其他xml架构模板

时间:2011-06-14 09:52:57

标签: xslt xsd

任何人都可以给我一个转换xml架构模板的范例,比如

    <xs:element name="carareWrap">
    <xs:annotation>
      <xs:documentation xml:lang="en">The CARARE wrapper element. It wraps CARARE elements.</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="1" maxOccurs="unbounded" ref="carare"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

使用xsl的其他xml架构模板? 另一个xml架构可以是你可以做的任何东西..我只需要开始使用somwthing ...

2 个答案:

答案 0 :(得分:1)

  

任何人都可以给我一个将xml架构模板转换为其他xml架构模板的范例吗?

XML Schema只是一个声明名称空间为uri http://www.w3.org/2001/XMLSchema的XML文档。因此,您可以照常应用XSLT。

例如,您有这样的源架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="carareWrap">
    <xs:annotation>
        <xs:documentation xml:lang="en">The CARARE wrapper element. It wraps CARARE elements.</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="unbounded" ref="carare"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

并且(例如)您只想删除引用元素的属性。您可以应用以下转换:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="1.0">

    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="xs:element[@ref]">
        <xsl:copy>
            <xsl:copy-of select="@ref"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

结果将是:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="carareWrap">
        <xs:annotation>
            <xs:documentation xml:lang="en">The CARARE wrapper element. It wraps CARARE elements.</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="carare" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

<强>通知

  • 需要在XSLT中声明输入文档的命名空间
  • 使用身份转换按原样复制输入文档,并按要求覆盖元素。

答案 1 :(得分:0)

跟踪XSD文档没什么特别之处。它们只是XML。

由于您未指定要进行的更改,因此以下是一个示例XSLT样式表,用于更改随机详细信息(在这种情况下为minOccurs的值)

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <!-- the identity template copyies everything as-is -->
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>

  <!-- ...unless there is a more specific template available -->
  <xsl:template match="
    xs:element[@name = 'carareWrap']//xs:element[@ref = 'carare' and @minOccurs = 1]/@minOccurs
  ">
    <xsl:attribute name="{name()}">2</xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

输出

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="carareWrap">
    <xs:annotation>
      <xs:documentation xml:lang="en">The CARARE wrapper element. It wraps CARARE elements.</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="2" maxOccurs="unbounded" ref="carare"></xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

需要注意的一些事项:

  • 命名空间声明xmlns:xs="http://www.w3.org/2001/XMLSchema"所以xs前缀在XSLT样式表中可用
  • 使用身份模板复制未处理的所有内容
  • 使用复杂匹配表达式来选择特定节点
  • 使用属性值模板name()函数复制属性名称:name="{name()}"