【问题标题】:Xml schema issue - cannot work out how to use key!XML 架构问题 - 无法确定如何使用密钥!
【发布时间】:2009-09-20 07:44:52
【问题描述】:

我正在尝试为以下内容创建一个 xml 架构:

<tagSet>
  <Structure>
    <tag>
      <name>Steve</name>
    </tag>
    <tag>
      <name>Bob</name>
    </tag>
    <tag>
      <name>Steve</name>
    </tag>
  </Structure>
</tagSet>

我希望我的架构抱怨史蒂夫在那里两次,但我无法让它工作。

我在架构文件的 tagSet 元素下有这个:

<xs:key name="key" >
      <xs:selector xpath="Structure/tag" />
      <xs:field xpath="name" />
    </xs:key>

...但我显然没有正确理解它,因为这不起作用。有人发现我的错误吗?

谢谢:)

【问题讨论】:

    标签: xml xsd schema


    【解决方案1】:

    您的 xs:key 定义没有任何错误。您是否在 XML 文件中正确引用了 XSD?

    我将您的 XML 数据复制到一个文档中:

    <?xml version="1.0" encoding="utf-8" ?>
    <tagSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="test.xsd">
      <Structure>
        <tag>
          <name>Steve</name>
        </tag>
        <tag>
          <name>Bob</name>
        </tag>
        <tag>
          <name>Steve</name>
        </tag>
      </Structure>
    </tagSet>
    

    然后我用你的 xs:key 写了一个简单的 XSD:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <xs:element name="tagSet">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Structure" type="Structure-type" />
          </xs:sequence>
        </xs:complexType>
    
        <xs:key name="key">
          <xs:selector xpath="Structure/tag" />
          <xs:field xpath="name" />
        </xs:key>
      </xs:element>
    
      <xs:complexType name ="Structure-type">
        <xs:sequence>
          <xs:element name ="tag" type="tag-type" maxOccurs="unbounded" />
        </xs:sequence>
      </xs:complexType>
    
      <xs:complexType name="tag-type">
        <xs:sequence>
          <xs:element name="name" type="xs:string" />
        </xs:sequence>
      </xs:complexType>
    
    </xs:schema>
    

    它抱怨重复键“史蒂夫”很好。如果此示例不能帮助您找到问题,您能否发布有关您的架构和 XML 文件的更多详细信息?

    【讨论】:

    • 谢谢 - 我很懒惰并使用 Visual Studio 为我创建了一个架构,然后添加了密钥。看起来 VS 设法混合了命名空间引用 - 现在已经开始工作了。
    【解决方案2】:

    你试过了吗:

    <xs:key name="key" >
      <xs:selector xpath=".//Structure/tag" />
      <xs:field xpath="name" />
    </xs:key>
    

    ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 2021-04-26
      相关资源
      最近更新 更多