xmlschema格式

时间:2012-05-28 09:11:17

标签: xml xsd

为xml元素定义架构的正确格式是什么,其中包含属性,子元素和子元素也包含SUB-ELEMENTS
例如:我的xml

<element1 attribute1="hello">
 <element-sub1>
       <element-sub-sub1 attribute-sub-1="hi"/>
 <elementsubsub1>
</element1>

我尝试使用以下架构

         <xs:element name="element1">
         <xs:complexType>
         <xs:sequence>
         <xs:element name="element-sub1" type="xs:anyType" maxOccurs="unbounded"/>
          <xs:complexType> 
          <xs:sequence>
      <xs:element name="element-sub-sub1" type="xs:anyType" maxOccurs="unbounded"/>
      </xs:sequence>
    <xs:attribute name="attribute-sub-1" type="xs:string"/>
      </xs:complexType>

          </xs:sequence>
      <xs:attribute name="attribute1" type="xs:string"/>
      </xs:complexType>
       </xs:element>

但是我收到以下错误

The content of 'sequence' must match (annotation?, (element | group | choice | sequence | any)*). A problem was found st
arting at: complexType.

为什么我收到此错误?为我的要求编写模式的正确格式是什么?
注意
元素“element-sub-sub1”也可以包含文本 更新1

<element1 URI="">
 <element-sub1>
 <element-sub-sub1 Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
 <element-sub-sub1 Algorithm="http://www.w3.org/TR/1999/REC-xslt-19991116">
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output method="text"/>
<xsl:template match="/">
Pan : <xsl:copy-of select="//Pp"/>

MobileNo : <xsl:copy-of select="//Mm"/>

TotalAmount : <xsl:copy-of select="//Total"/>
</xsl:template>
</xsl:stylesheet>
 element-sub-sub1
 </element-sub1>
 </element1>

2 个答案:

答案 0 :(得分:1)

您的架构无效,甚至格式正确。这就是你需要的:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:test:1">

   <xs:element name="element1">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="element-sub1" maxOccurs="unbounded">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="element-sub-sub1" maxOccurs="unbounded">
                        <xs:complexType>
                           <xs:attribute name="attribute-sub-1" type="xs:string" />
                        </xs:complexType>
                     </xs:element>
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
         <xs:attribute name="attribute1" type="xs:string" />
      </xs:complexType>
   </xs:element>

</xs:schema>

答案 1 :(得分:1)

首先,您不能在同一type="xs:anyType"

上拥有<xs:complexType>属性和<xs:element>元素

其次,<xs:complexType>的定义可能只会立即显示在<xs:element>内,或作为<schema>

的子项显示为全局类型

最后,但并非最不重要。如果希望元素包含属性,请使其类型复杂。

    <xs:element name="element1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="element-sub1" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="element-sub-sub1" maxOccurs="unbounded">
                            <xs:complexType mixed="true">
                                <xs:any minOccurs="0" maxOccurs="1"/>
                                <xs:attribute name="attribute-sub-1" type="xs:string" />
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="attribute1" type="xs:string" />
    </xs:complexType>
</xs:element>

如评论中所述,该元素允许您在该位置插入任何xml元素。无论元素是否根据某些标准实际有效,验证都将通过。它必须是良好的形式。

如果要验证整个样式表,请使用xs:import来访问命名空间,其中定义了xsl样式表:http://www.w3.org/XML/2000/04schema-hacking/xslt.xsd并引用xsd中的样式表或转换元素。 在<xs:element name="sub-sub1>内:

                            <xs:complexType mixed="true">
                                <xs:choice minOccurs="0" maxOccurs="1">
                                    <xs:element ref="xsl:stylesheet"/>
                                    <xs:element ref="xsl:transform"/>
                                <xs:choice>
                                <!-- You'll have to define a prefix for the xslt namespace imported -->
                                <xs:attribute name="attribute-sub-1" type="xs:string" />
                            </xs:complexType>

choice元素允许您为XSL样式表使用两个可接受的顶级标记之一,<stylesheet&gt;或<transform>

UPDATE:为可选的样式表/变换添加了minOccurs,maxOccurs属性