Search code examples
xsdmarklogic

Uploading XSD schemas with imported namespaces into Marklogic 6


As a small excercise I'm trying to upload and validate documents with a couple of schemas. These will eventually grow so I'd like to keep them separate.

These example schemas define a localized text string and a food dish:

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>

I then uploaded these two documents inside the Schemas database assigned to my xml database with the following URI id's:

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

And then I uploaded an example document:

dish/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>

When I issue the following command on the server's query console, I get the empty set when I instruct the database to infer the type of the xml document's root node:

Update: wrong node index, must be 1 in this case:

(: 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]
))

Output:

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

This means that the database is either ignoring or not able to find my schema documents. Also, I am not sure if it can't apply the schemas because of the xs:import namespace statement.

Has anyone attempted to work with imported XSD documents into Marklogic in this way? Is there anything I may be doing wrong?

Update 2: It seems this can only be applied to attributes. The following command does work as expected, which means the schemas are being detected:

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

Thanks!


Solution

  • XQuery positional predicates start at 1 not 0. Try this:

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

    Then work your way up to more complicated code.

    -David Lee