如何在与XML数据相同的文件中包含XML模式声明?

时间:2011-09-27 23:20:25

标签: xml xsd

你是怎么做到的?

这是我的参考尝试,但我认为我没有正确地做到这一点:

<?xml version="1.0" encoding="UTF-8" ?>

<X:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://x.iaesr.com">
    <X:element name="track">
        <X:complexType>
            <X:sequence>
                <X:element name="title" type="X:string" />
                <X:element name="length" type="X:time" use="optional" />
                <X:element name="featuring" type="X:string" use="optional" />
            </X:sequence>
            <X:attribute name="track_number" type="X:int" use="optional" />
        </X:complexType>
    </X:element>
    <X:element name="cd_collection">
        <X:complexType>
            <X:sequence>
                <X:element name="album" minOccurs="1">
                    <X:complexType>
                        <X:sequence>
                            <X:element name="id" type="X:int" />
                            <X:element name="title" type="X:string" />
                            <X:element name="artist" type="X:string" />
                        </X:sequence>
                        <!--
                            The following use of of the schema:choice element makes having a Disk item in the
                            Album element optional. In either case, it ensures that an album contains either
                            Disk elements with Track elements in them, or just Track elements alone, but never
                            both at the same level.
                        -->
                        <X:choice>
                            <X:element name="disk" minOccurs="1">
                                <X:complexType>
                                    <X:sequence>
                                        <X:element ref="track" minOccurs="1" />
                                    </X:sequence>
                                    <X:attribute name="id" type="X:int" />
                                </X:complexType>
                            </X:element>
                            <X:element ref="track" minOccurs="1" />
                        </X:choice>
                        <X:attribute name="label" type="X:string" />
                    </X:complexType>
                </X:element>
            </X:sequence>
        </X:complexType>
    </X:element>
</X:schema>

<cd_collection xmlns="http://x.iaesr.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="joePeaCDCollection.xsd">
    <album label="Raisin Records">
    <disk id="1">
        <id>1</id>
        <title>Barenaked Ladies Are Men</title>
        <artist>Barenaked Ladies</artist>
        <track track_number="1">
            <title>Serendipity</title>
            <length>00:04:11</length>
        </track>
        <track track_number="2">
            <title>Something You'll Never Find</title>
            <length>00:04:57</length>
        </track>
        <track track_number="3">
            <title>One and Only</title>
            <length>00:03:47</length>
        </track>
        <track track_number="4">
            <title>Angry People</title>
            <length>00:04:01</length>
        </track>
        <track track_number="5">
            <title>Down To Earth</title>
            <length>00:03:46</length>
        </track>
        <track track_number="6">
            <title>Beautiful</title>
            <length>00:02:35</length>
        </track>
        <track track_number="7">
            <title>Running Out Of Ink</title>
            <length>00:03:58</length>
        </track>
        <track track_number="8">
            <title>Half a Heart</title>
            <length>00:04:27</length>
        </track>
        <track track_number="9">
            <title>Maybe Not</title>
            <length>00:03:00</length>
        </track>
        <track track_number="10">
            <title>I Can I Will I Do</title>
            <length>00:03:08</length>
        </track>
        <track track_number="11">
            <title>Fun &amp; Games</title>
            <length>00:03:45</length>
        </track>
        <track track_number="12">
            <title>The New Sad</title>
            <length>00:02:34</length>
        </track>
        <track track_number="13">
            <title>Quality</title>
            <length>00:04:15</length>
        </track>
        <track track_number="14">
            <title>Another Spin</title>
            <length>00:04:05</length>
        </track>
        <track track_number="15">
            <title>What A Letdown</title>
            <length>00:03:49</length>
        </track>
        <track track_number="16">
            <title>Why Say Anything Nice?</title>
            <length>00:03:42</length>
        </track>
    </disk>
    </album>
    <album label="Virgin Records">
        <id>2</id>
        <title>Live From Mars</title>
        <artist>Ben Harper</artist>
        <disk id="1">
            <track track_number="1">
                <title>Glory &amp; Consequence</title>
            </track>
            <track track_number="2">
                <title>Excuse Me Mr.</title>
            </track>
            <track track_number="3">
                <title>Alone</title>
            </track>
            <track track_number="4">
                <title>Sexual Healing</title>
            </track>
            <track track_number="5">
                <title>Woman in You</title>
            </track>
            <track track_number="6">
                <title>Ground on Down</title>
            </track>
            <track track_number="7">
                <title>Steal My Kisses</title>
            </track>
            <track track_number="8">
                <title>Burn One Down</title>
            </track>
            <track track_number="9">
                <title>Mama's Got a Girlfriend</title>
            </track>
            <track track_number="10">
                <title>Welcome to the Cruel World</title>
            </track>
            <track track_number="11">
                <title>Forgiven</title>
            </track>
            <track track_number="12">
                <title>Faded/Whole Lotta Love</title>
            </track>
        </disk>
        <disk id="2">
            <track track_number="1">
                <title>Waiting on an Angel</title>
            </track>
            <track track_number="2">
                <title>Roses from My Friends</title>
            </track>
            <track track_number="3">
                <title>Power of the Gospel</title>
            </track>
            <track track_number="4">
                <title>Pleasure and Pain</title>
            </track>
            <track track_number="5">
                <title>Please Bleed</title>
            </track>
            <track track_number="6">
                <title>The Drugs Don't Work</title>
                <featuring>Richard Ashcroft</featuring>
            </track>
            <track track_number="7">
                <title>In the Lord's Arms</title>
            </track>
            <track track_number="8">
                <title>Not Fire, Not Ice</title>
            </track>
            <track track_number="9">
                <title>Beloved One</title>
            </track>
            <track track_number="10">
                <title>Number Three</title>
            </track>
            <track track_number="11">
                <title>Walk Away</title>
            </track>
            <track track_number="12">
                <title>Another Lonely Day</title>
            </track>
            <track track_number="13">
                <title>Like a King/I'll Rise</title>
            </track>
        </disk>
    </album>
</cd_collection>

2 个答案:

答案 0 :(得分:0)

John Saunders是对的,没有预先确定的方法。但是,如果您确实需要这个,则可以在一个文件中为包含模式和实例的文档定义模式。 Web Services Description Language中使用了类似的方法。这是一个简洁的想法:

schema.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="instance">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="types" type="Types"/>
            <xsd:element name="document" type="Document"/>
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
   <xsd:complexType name="Types">
      <xsd:sequence>
         <xsd:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="Document">
      <xsd:sequence>
         <xsd:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

instance.xml

<?xml version="1.0" encoding="UTF-8"?>
<instance xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='schema.xsd'>
   <types>
      <schema xmlns="http://www.w3.org/2001/XMLSchema" targetnamespace="library">
         <element name="book">
            <complexType>
               <sequence>
                  <element name="title" type="string"/>
                  <element name="author" type="string"/>
               </sequence>
            </complexType>
         </element>
      </schema>
   </types>
   <document>
      <book xmlns="library">
         <title>Memoirs</title>
         <author>John Smith</author>
      </book>
   </document>
</instance>

稍后您将从<types>读取架构并使用它来验证<document>的内容。

答案 1 :(得分:-1)

没有一般的方法可以做到这一点,因为这通常是一个坏主意。即使您可能已经在DataSet中看到过这种情况,“请不要在家中试试这个”。