首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我不能在XML架构中包含属性。(已使用http://www.w3.org/2001/XMLSchema-instance)

我不能在XML架构中包含属性。(已使用http://www.w3.org/2001/XMLSchema-instance)
EN

Stack Overflow用户
提问于 2012-04-20 17:12:58
回答 2查看 2.1K关注 0票数 1

我第一次尝试创建一个XML Schema来验证我的XML。

我的XML的开始(注意"-instance“和"SectionNumber="0""):

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<CrystalReport 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="urn:crystal-reports:schemas:report-detail  
                   http://www.businessobjects.com/products/xml/CR2008Schema.xsd" 
  xmlns="urn:crystal-reports:schemas:report-detail"
>

  <ReportHeader>
    <Section SectionNumber="0">
      <Text Name="Text9">
        ...

我的XML模式(请注意第2、3和20行):

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<xsi:schema  id="XMLSchema_varslings1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="urn:crystal-reports:schemas:report-detail"
    elementFormDefault="qualified"
    targetNamespace="urn:crystal-reports:schemas:report-detail">

    <xsi:element name="CrystalReport" type="CrystalReportType"/>

    <xsi:complexType name="CrystalReportType">
        <xsi:sequence maxOccurs="unbounded">
            <xsi:element name="ReportHeader"  type="ReportHeaderType"/>
        </xsi:sequence>
    </xsi:complexType>

    <xsi:complexType name="ReportHeaderType">
        <xsi:sequence>
            <xsi:element name="Section" type="SectionType"/>
            <!-- This is line 19....................................... -->
            <xsi:attribute name="SectionNumber" type="xsi:Integer"/>
        </xsi:sequence>
    </xsi:complexType>

    <xsi:complexType name="SectionType">
        <xsi:sequence maxOccurs="unbounded">
            <xsi:element name="Text" type="TextType" />
        </xsi:sequence>
    </xsi:complexType>

    <xsi:complexType name="TextType">
        <xsi:sequence maxOccurs="unbounded">
            <xsi:element name="TextValue" type="xsi:string" />
        </xsi:sequence>
    </xsi:complexType>
</xsi:schema>

我得到了这个错误,我不能解决:"The root element of a W3C XML Schema should be <schema> and its namespace should be 'http://www.w3.org/2001/XMLSchema'.

如果我从模式中删除了"-instance“,我就摆脱了上面的错误,但是我就不能使用属性"<xsi:attribute name="SectionNumber" type="xsi:Integer"/>”的代码了。

我甚至不知道我真正的问题是-instance部分,还是有其他方法可以在模式中写入/包含属性。我该如何解决这个问题呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-20 18:13:23

您混淆了两个名称空间:XMLSchemaXMLSchema-instance。这两种方法的用途不同,XMLSchema (通常带有前缀xs)用于声明您的模式。这就是没有它XSD文件无法工作的原因。

当您希望在文档中使用某些XML Schema时,可以使用名称空间XMLSchema-instance (通常带有前缀xsi)。例如,schemaLocation前缀位于此命名空间中。

类型integer (小写i)在XMLSchema名称空间中,因此您必须这样使用它。

此外,元素的顺序也不正确。<xs:sequence>之后应该是<xs:attribute>

因此,您的模式应该如下所示:

代码语言:javascript
复制
<xs:schema id="XMLSchema_varslings1"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:crystal-reports:schemas:report-detail"
    elementFormDefault="qualified"
    targetNamespace="urn:crystal-reports:schemas:report-detail">

  …

  <xs:complexType name="ReportHeaderType">
    <xs:sequence>
      <xs:element name="Section" type="SectionType"/>
    </xs:sequence>
    <xs:attribute name="SectionNumber" type="xs:integer"/>
  </xs:complexType>

</xs:schema>

我还将名称空间前缀更改为xs,因为这很有意义,但从技术上讲,这并不是必需的。

票数 1
EN

Stack Overflow用户

发布于 2012-04-20 17:43:50

嗯,我发现了这个:

Link to w3

“此架构永远不能这样使用: XML架构建议禁止在此命名空间中声明属性”

我想我有麻烦了,因为我需要修改XML。问题是XML是从我们已有的旧程序中生成的。有没有人能确认/不能确认我找到了解决方案,否则我会推荐任何人的答案?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10243341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档