0

作为一个小练习,我正在尝试使用几个模式上传和验证文档。这些最终会增长,所以我想将它们分开。

这些示例模式定义了一个本地化的文本字符串和一个食物盘:

bitfood-common.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:bfc="http://bitfood.org/common" 
  targetNamespace="http://bitfood.org/common"
  elementFormDefault="qualified">

  <xs:simpleType name="objectId">
    <xs:restriction base="xs:string">
      <xs:length value="24"/>
      <xs:whiteSpace value="collapse"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="locale">
    <xs:restriction base="xs:string">
      <xs:length value="5"/>
      <xs:whiteSpace value="collapse"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="localizedText">
    <xs:attribute name="text" type="xs:string" use="required"/>
    <xs:attribute name="locale" type="bfc:locale" use="required"/>
  </xs:complexType>
</xs:schema>

bitfood-dish.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:bfd="http://bitfood.org/dish" 
  xmlns:bfc="http://bitfood.org/common"
  targetNamespace="http://bitfood.org/dish" 
  elementFormDefault="qualified">

  <xs:import namespace="http://bitfood.org/common" 
  schemaLocation="../common/bitfood-common.xsd" />

  <xs:element name="Dish">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="bfc:localizedText" maxOccurs="unbounded"/>
        <xs:element name="description" type="bfc:localizedText" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="id" type="bfc:objectId" use="required"/>
      <xs:attribute name="price" type="xs:decimal" use="required"/>
      <xs:attribute name="imageUrl" type="xs:anyURI" use="required"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

然后,我将这两个文档上传到分配给我的 xml 数据库的 Schemas 数据库中,并具有以下 URI id:

schema/common/bitfood-common.xsd
schema/dish/bitfood-dish.xsd

然后我上传了一个示例文档:

菜/520cc720c208c01ddfb75254.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Dish id="520cc720c208c01ddfb75254" price="35" 
      imageUrl="FoodCantonGourrmetTwoDish.jpg" 
      xmlns="http://bitfood.org/dish">
  <name text="Example dish." locale="en-US"/>
  <description text="Localized text test." locale="en-US"/>
</Dish>

当我在服务器的查询控制台上发出以下命令时,当我指示数据库推断 xml 文档的根节点的类型时,我得到了空集:

更新:错误的节点索引,在这种情况下必须为 1:

(: is it working? :)
declare namespace bfd = "http://bitfood.org/dish";
declare namespace bfc = "http://bitfood.org/common";
xdmp:describe(data(
  doc('dish/520cc720c208c01ddfb75254.xml')/bfd:Dish[1]
))

输出:

<?xml version="1.0" encoding="UTF-8"?>
<results warning="atomic item">xs:untypedAtomic("")</results>

这意味着数据库忽略或无法找到我的模式文档。另外,我不确定它是否因为该xs:import namespace语句而无法应用模式。

有没有人试图以这种方式将导入的 XSD 文档处理到 Marklogic 中?有什么我可能做错了吗?

更新 2:似乎这只能应用于属性。以下命令按预期工作,这意味着正在检测模式:

(: is it working? :)
declare namespace bfd = "http://bitfood.org/dish";
declare namespace bfc = "http://bitfood.org/common";
xdmp:describe(data(doc('dish/520cc720c208c01ddfb75254.xml')/bfd:Dish[1]/@id))

谢谢!

4

1 回答 1

2

XQuery 位置谓词从 1 而不是 0 开始。试试这个:

doc('dish/520cc720c208c01ddfb75254.xml')/bfd:Dish[1]

然后按照自己的方式处理更复杂的代码。

——大卫·李

于 2013-08-16T02:05:40.177 回答