xml模式格式

标签 xml xsd

为包含属性、子元素和子元素还包含子元素的 xml 元素定义架构的正确格式是什么
例如:我的 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>

最佳答案

首先,你不能拥有 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 > 或<transform>

更新:为可选样式表/转换添加了 minOccurs、maxOccurs 属性

关于xml模式格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10782069/

相关文章:

c# - 如何将此 XPath 查询转换为 LINQ to XML?

android - 如何将两种不同的样式应用于android中的一个元素?

c# - 在 XDocument 中按名称在任意深度查询元素

xml - 组织 XML 模式文件的最佳实践

正则表达式匹配偶数、非零数字

java - Jaxb 编码 Point 变量

java - RecyclerView 中的 Android Spinner 选中时获取当前 View

xml - XSD:检查是否存在具有特定值的元素

xml - 元素 "xsd"的前缀 "xsd:schema"未绑定(bind)

ant - 如何使用jaxb和Ant xjc任务处理多个xsd模式?