2

属性、サブ要素、およびサブ要素を含む xml 要素のスキーマを定義するための適切な形式は何ですか?
例: My 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>
4

2 に答える 2

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>
于 2012-05-28T09:25:58.120 に答える
1

type="xs:anyType"まず、属性と<xs:complexType>要素を同じにすることはできません<xs:element>

第 2 に、 の定義は、または の子としてのグローバル タイプの<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 スタイルシートで受け入れられている 2 つのトップ タグのいずれかを使用できます。<stylesheet> または<transform>

更新: オプションのスタイルシート/変換の minOccurs、maxOccurs 属性を追加しました

于 2012-05-28T09:26:04.830 に答える