开发者

transforming with xsl an xml schema template to an other xml schema template

开发者 https://www.devze.com 2023-03-13 07:14 出处:网络
can anyone give me a paradigm of transforming an xml schema template like <xs:element name=\"carareWrap\">

can anyone give me a paradigm of transforming an xml schema template like

    <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>

to an other xml schema template with xsl? the other xml schema could开发者_如何学编程 be anything you can do.. I just need to have somwthing to start with...


can anyone give me a paradigm of transforming an xml schema template like ... to an other xml schema template?

An XML Schema is just a an XML document with declared namespace uri http://www.w3.org/2001/XMLSchema. Therefore, you can apply XSLT as usual.

For instance, you have a source schema like this:

<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>

And (for example) you want to remove the attributes of reference elements only. You can apply the following transform:

<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>

The result will be:

<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>

Notice

  • The needing of the declaration of the namespace of the input document in the XSLT
  • The usage of the identity transform to copy the input document as is and override the elements as by requirements.


There is nothing special about trating XSD documents. They are just XML.

Since you do not specify what changes you want to make, here is a sample XSLT stylesheet that changes a random detail (the value of minOccurs in this case)

<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>

Output

<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>

A few things to notice:

  • the namespace declaration xmlns:xs="http://www.w3.org/2001/XMLSchema" so the xs prefix is available in the XSLT stylesheet
  • the use of the identity template copy everything that is not handled otherwise
  • the use of a complex match expression to pick a specific node
  • the use of an attribute value template and the name() function to copy the attribute name: name="{name()}"
0

精彩评论

暂无评论...
验证码 换一张
取 消