将带有导入名称空间的XSD架构上载到Marklogic 6中

时间:2013-08-16 01:52:50

标签: xsd marklogic

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

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

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标识:

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))

谢谢!

1 个答案:

答案 0 :(得分:2)

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

DOC( '菜/ 520cc720c208c01ddfb75254.xml')/ BFD:菜[1]

然后逐步完成更复杂的代码。

-David Lee