2

I'd been 3 years since I messed with schemas and I remember them being very particular about name space prefixes and all. I also remember complex types not parsing so easily without unrolling when using the XML editor in Eclipse (I have Indigo).

So could one is you XML gurus look at my XML file and tell me what is wrong with the prepended XSD XML schema?

Thanks.

Ciao

+++++

Eclipse complains of:

The markup in the document following the root element must be well-formed.

I should point out I created the XML by hand and autogenerated the XML from it at:

http://www.xmlforasp.net/codebank/system_xml_schema/buildschema/buildxmlschema.aspx

(Seperate Complex Types: as I did manually 3 years ago with Amazon's Amazon Seller Central XML files manually)

If seems to me as ref should be used in one case but I'll defer to the experts.

+++++

<?xml version="1.0" encoding="utf-16"?>

<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="idioms" type="idiomsType" />
    <xsd:complexType name="idiomsType">
        <xsd:sequence>
            <xsd:element maxOccurs="unbounded" name="idiom" type="idiomType" />
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="idiomType">
        <xsd:sequence>
            <xsd:element name="phrase" type="xsd:string" />
            <xsd:element name="meaning" type="xsd:string" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

<idioms>
    <idiom>
        <phrase>A bit much</phrase>
        <meaning>Excessive or annoying.</meaning>
    </idiom>
    <idiom>
        <phrase>A bridge too far</phrase>
        <meaning>Act of overreaching.</meaning>
    </idiom>
</idioms>

OK, update. My shortcut of including XSD inline with XML is illegal. So I put it back in XSD file. I also learned that I can't start a prefix with xml so I changed it to 'tc'.

The XSD now is correct (in the same style as I unrolled Amazon Seller Central XSDs).

<?xml version="1.0" encoding="utf-16"?>

<xsd:schema xmlns:tcidioms="urn:tcidioms" targetNamespace="urn:tcidioms" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <xsd:element name="idioms" type="tcidioms:idiomsType"/>
     <xsd:complexType name="idiomsType">
         <xsd:sequence>
             <xsd:element maxOccurs="unbounded" name="idiom" type="tcidioms:idiomType" />
         </xsd:sequence>
     </xsd:complexType>
     <xsd:complexType name="idiomType">
         <xsd:sequence>
             <xsd:element name="phrase" type="xsd:string" />
             <xsd:element name="meaning" type="xsd:string" />
         </xsd:sequence>
     </xsd:complexType>
</xsd:schema>

But I have trouble trying to include this im my XML file which now looks like this (the XML & XSD are in the same directory):

It complains: No grammar constraints (DTD or XML schema) detected for the document.

Meaning the 2 line in my XML document isn't correct. I've tried several variants to include this file but none have worked. Keep in mine this XML (and XSD) is going to be read off a local file systems rather than via http. Will it still validate in this manner?

<?xml version="1.0" encoding="utf-16"?>

<idioms xmlns:tcidioms="urn:tcidioms" targetNamespace="urn:tcidioms ./EnglishIdioms.xsd">
    <idiom>
        <phrase>A bit much</phrase>
       <meaning>Excessive or annoying.
       </meaning>
    </idiom>
    <idiom>
         <phrase>A bridge too far</phrase>
   <meaning>Act of overreaching, going too far
        and getting into trouble
        or
        failing.
   </meaning>
    </idiom>
</idioms>

Visual Studio 2010 (it created the schema Russian Doll Style):

The XML file although set to use the schema in the VS2010 interface doesn't show that in the code?

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

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="idioms">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="idiom">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="phrase" type="xs:string" />
                            <xs:element name="meaning" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

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

<idioms>
    <idiom>
        <phrase>A bit much</phrase>
        <meaning>Excessive or annoying.</meaning>
    </idiom>
    <idiom>
        <phrase>A bridge too far</phrase>
        <meaning>Act of overreaching.</meaning>
    </idiom>
    <idiom>
        <phrase>A chain is no stronger than its weakest link</phrase>
        <meaning>The weakest part of an object is it's strength.</meaning>
    </idiom>
</idioms>
4

3 回答 3

3

您的根元素未正确关闭。

使用</idioms>,而不是<idioms>:)

更新:

缺少命名空间可能是问题(在模式和 xml 中)。

于 2012-04-09T06:38:49.987 回答
1

XSD 规范

由顶级声明·已验证·元素信息项必须用该声明的{目标名称空间}进行限定(如果这是·不存在·,则该项必须是不限定的)。

翻译:

如果您想使用elementFormDefault设置为限定的属性(您是故意选择的吗?),那么您必须声明一个targetNamespace元素并将其设置为某个 URI,然后为其提供也在该命名空间中声明的 XML。所以,像

<xsd:schema targetNamespace="urn:so:example" attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
</xsd:schema>

并在 xml 中使用该命名空间:

<idioms xmlns="urn:so:example">
  <idiom>
    <phrase>A bit much</phrase>
    <meaning>Excessive or annoying.</meaning>
  </idiom>
  <idiom>
    <phrase>A bridge too far</phrase>
    <meaning>Act of overreaching.</meaning>
  </idiom>
</idioms>

或者,我认为您可以elementFormDefault完全从方案标签中删除该属性。它的默认值是不合格的。

于 2012-04-09T17:58:31.503 回答
0

我使用 VS2010 从我的 XML 生成 XML 模式。在这样做时,它似乎忽略了(私有)命名空间,但它确实将 UI 中的模式设置为它刚刚生成的 XML 模式的名称。

此外,它不会在 XML 本身中显示它使用或包含此 XML 模式。

这是因为 XML 和 XSD 文件都具有相同的基本名称(EnglishIdioms.)并且这两个文件都在同一个目录中吗?

于 2012-04-10T21:50:50.193 回答