Default Schema Values in MarkLogic

狂风中的少年 提交于 2019-12-11 13:16:42

问题


I'm trying to insert a document via xdmp:document-insert() before that is called I am validating the document against it's respective schema via validate strict { $xml } and using that output in the insert call. However the output of the validate call does not include the default value specified in the schema.

Simplified schema:

<xs:schema>
  <xs:complexType name="fields-type" abstract="false" mixed="false">
    <xs:sequence minOccurs="1" maxOccurs="1">
      <xs:element default="Faulkner" maxOccurs="1" minOccurs="0" name="an_author" nillable="false" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="document-type" abstract="false" mixed="false">
    <xs:sequence minOccurs="1" maxOccurs="1">
      <xs:element name="fields" type="fields-type" minOccurs="1" maxOccurs="1" nillable="false"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="document" type="document-type" abstract="false" nillable="false"/>
</xs:schema>

The document:

 <document>
     <fields>
       <an_author/>
     </fields>
 </document>

After calling validate strict { $xml } the output document is the same as above with no default value added to the <an_author> element. Note: I also tried using the fixed attribute in place of default in the schema and I'm getting the same result. xdmp:validate($xml, "strict") isn't returning any errors either.

Edit: According to the XQuery validate specification here the output should have the default value specified.


回答1:


The default values are in fact part of the data model, but they aren't necessarily serialized when we output the data model. You can verify that the default attributes are in the data model by doing a path expression against them.

If you want to make sure they get serialized on output, there is an output setting that will force them to be emitted:

declare option xdmp:output "default-attributes=yes";

(Or you can set the option default-attributes on xdmp:quote or xdmp:save.)

Alternatively, you can force a copy of the data model instance, which will carry all the attributes along, but forgets that they were defaulted:

let $d := validate strict { $node }
return document { $d }


来源:https://stackoverflow.com/questions/13143530/default-schema-values-in-marklogic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!