【问题标题】:How to define a Xpath-selector in xml schema for a recursive type如何在 xml 模式中为递归类型定义 Xpath 选择器
【发布时间】:2010-10-27 07:00:41
【问题描述】:

编辑:添加键。

嗨,

我有以下类型的 xml 架构:

<xs:complexType name="definition">
  <xs:sequence/>
  <xs:attribute name="id"/>
</xs:complexType>

<xsd:key name="definitionId">
  <xsd:selector xpath="definition"/>
  <xsd:field xpath="@id"/>
</xsd:key>

<xs:complexType name="elem">
  <xs:sequence>
    <xs:element name="entry1" type="elem" minOccurs="0" maxOccurs="unbounded"/>
    <xs:element name="entry2" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
  <xs:attribute name="ref" type="xs:string" use="required"/>
</xs:complexType>

这允许类似:

<definition id="A">
<definition id="B">
<definition id="C">

<entry1 ref="A">
  <entry1 ref="B">
    <entry1 ref="C"/>
    <entry2/>
  </entry1>
  <entry1 ref="C">
  </entry1>
</entry1>

我需要一个 XPath 选择器来声明 ref 属性的 keyref,但我不知道如何定义递归路径。

<xsd:keyref name="definitionRef" refer="definitionId">
  <xsd:selector xpath="???"/>  <<<how to find all "ref" of entry1 ?    
  <xsd:field xpath="@ref"/>
</xsd:keyref>

感谢您的宝贵时间。

【问题讨论】:

  • 你需要keyref吗?语义很重要。
  • 我正在定义一些带有 id 属性的元素。 id-attribute 被声明为一个键。我想保证 ref-attribute 的内容是在 id-attribute 中定义的。你明白我的意图还是我应该扩展这个例子?
  • @kasten:我看到的问题是,在您的示例中,您没有为 IDREF 提供 ID 或 KEY。
  • all "ref" of entry1 到底是什么意思? //entry1/@ref 不就行了吗?
  • // 不是 xml-schema 支持的 xpath 子集的一部分。我正在寻找一种等效的方法来做到这一点:(

标签: xml xsd


【解决方案1】:

&lt;xsd:selector&gt; 支持受限的 XPath 表达式。只允许使用 child 轴,但 XPath 表达式可以开始.//,因此您可以使用递归表达式 .//entry1

更多详细信息,请参阅规范:http://www.w3.org/TR/xmlschema-1/#c-selector-xpath

&lt;xsd:selector&gt; 元素包含一个 XML 路径语言 (XPath) 表达式,该表达式指定元素集,字段指定的值在该元素集上必须是唯一的,因此它不应引用属性 ref&lt;xsd:field&gt; 元素指的是在该集合中应该是唯一的值,因此它应该引用属性ref(如果您需要唯一的值组合,其余的fields 指向其他值) .

【讨论】:

  • 非常感谢。我在我的问题之前尝试了一段时间,但错过了领先的“。”。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
  • 2016-06-09
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
相关资源
最近更新 更多