简体   繁体   English

如何转换不同用途的基本XML模式?

[英]How to transform a base XML Schema for different uses?

This is more of question of technique or tools to use rather than a how-to. 这更多地是要使用的技术或工具的问题,而不是操作方法。

I have an XML Schema that is used by the development team for parsing input and output and automatic code generation. 我有一个XML Schema,开发团队使用它来解析输入和输出以及自动代码生成。

The schema is designed for developers. 该架构是为开发人员设计的。 However there are other uses for this schema that have different needs. 但是,此架构还有其他用途,它们具有不同的需求。 For example the developer schema is strongly typed, however the user schema will need to be weakly typed to allow wildcard replacement. 例如,开发人员模式是强类型的,但是用户模式将需要弱类型以允许通配符替换。 Another example are the appinfo annotations that are used by the developer tools. 另一个示例是开发人员工具使用的appinfo批注。 These would be stripped out of the user schema. 这些将从用户模式中剥离。

For example, this: 例如,这:

<xs:complexType name="ModelA">
 <xs:annotation id="ModelA" >
  <xs:appinfo>
    <dev_type>AObject</dev_type>
    <function id="AFunction">
      <parameters>
        <parameter>P1</parameter>
        <parameter>P2</parameter>
      </parameters>
    </function>
  </xs:appinfo>
  <xs:documentation>
    <description>A model. Used by A in Camelot.</description>
  </xs:documentation>
</xs:annotation>
<xs:complexContent>
  <xs:extension base="dbs:Models">
    <xs:sequence>
      <xs:element name="RoundTables" type="xs:decimal"/>
      <xs:element name="ClarkGable" type="xs:boolean"/>
      <xs:element name="SequinVests" type="xs:decimal"/>
      <xs:element name="Opera" type="xs:string"/>
    </xs:sequence>
  </xs:extension>
</xs:complexContent>

Would become this: 会变成这样:

<xs:complexType name="ModelA">
  <xs:annotation id="ModelA" >
   <xs:documentation>
     <description>A model. Used by A in Camelot.</description>
   </xs:documentation>
  </xs:annotation>
  <xs:complexContent>
   <xs:extension base="dbs:Models">
    <xs:sequence>
      <xs:element name="RoundTables" type="xs:string"/>
      <xs:element name="ClarkGable" type="xs:string"/>
      <xs:element name="SequinVests" type="xs:string"/>
      <xs:element name="Opera" type="xs:string"/>
    </xs:sequence>
   </xs:extension>
 </xs:complexContent>

Would XSLT be useful here? XSLT在这里有用吗? Or could I load the schema as XML and use XPath to find and delete/modify tags and attributes? 还是可以将架构作为XML加载并使用XPath查找和删除/修改标签和属性?

Yes, XSLT would be ideal for transforming from a base to a derived XSD. 是的,XSLT非常适合从基本的XSD转换为派生的XSD。

For example, this simple XSLT based on the identity transformation, 例如,基于身份转换的简单XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" 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:appinfo"/>

  <xsl:template match="@type">
    <xsl:attribute name="type">xs:string</xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

takes your input XSD, 输入您的输入XSD,

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="ModelA">
    <xs:annotation id="ModelA" >
      <xs:appinfo>
        <dev_type>AObject</dev_type>
        <function id="AFunction">
          <parameters>
            <parameter>P1</parameter>
            <parameter>P2</parameter>
          </parameters>
        </function>
      </xs:appinfo>
      <xs:documentation>
        <description>A model. Used by A in Camelot.</description>
      </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="dbs:Models">
        <xs:sequence>
          <xs:element name="RoundTables" type="xs:decimal"/>
          <xs:element name="ClarkGable" type="xs:boolean"/>
          <xs:element name="SequinVests" type="xs:decimal"/>
          <xs:element name="Opera" type="xs:string"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>

and transforms it to your output XSD, 并将其转换为输出XSD,

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:complexType name="ModelA">
      <xs:annotation id="ModelA">
         <xs:documentation>
            <description>A model. Used by A in Camelot.</description>
         </xs:documentation>
      </xs:annotation>
      <xs:complexContent>
         <xs:extension base="dbs:Models">
            <xs:sequence>
               <xs:element name="RoundTables" type="xs:string"/>
               <xs:element name="ClarkGable" type="xs:string"/>
               <xs:element name="SequinVests" type="xs:string"/>
               <xs:element name="Opera" type="xs:string"/>
            </xs:sequence>
         </xs:extension>
      </xs:complexContent>
   </xs:complexType>
</xs:schema>

as requested. 按照要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM