2016-04-08 17 views
2

は、私はこのXSDから属性名とタイプを抽出したいXSDスキーマから属性名と型を取得するXquery?

<!-- tutorial.xsd --> 
<xs:schema targetNamespace="http://marklogic.com/tutorial" 
attributeFormDefault="unqualified" 
elementFormDefault="unqualified" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="name" type="xs:token" 
    abstract="false" nillable="false"/> 
    <xs:element name="qualification" type="xs:token" 
    abstract="false" nillable="false"/> 
    <xs:element name="born" type="xs:date" 
    abstract="false" nillable="false"/> 
    <xs:element name="dead" type="xs:date" 
    abstract="false" nillable="false"/> 
    <xs:element name="isbn" type="xs:unsignedLong" 
    abstract="false" nillable="false"/> 
    <xs:attribute name="id" type="xs:ID"/> 
    <xs:attribute name="available" type="xs:boolean"/> 
    <xs:attribute name="lang" type="xs:language"/> 
    <xs:element name="title" abstract="false" nillable="false"> 
    <xs:complexType mixed="false"> 
     <xs:simpleContent> 
    <xs:extension base="xs:token"> 
     <xs:attribute ref="lang" use="optional"/> 
    </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="library" abstract="false" nillable="false"> 
    <xs:complexType mixed="false"> 
     <xs:sequence minOccurs="1" maxOccurs="1"> 
    <xs:element ref="book" maxOccurs="unbounded" minOccurs="1"/> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="author" abstract="false" nillable="false"> 
    <xs:complexType mixed="false"> 
     <xs:sequence minOccurs="1" maxOccurs="1"> 
    <xs:element ref="name" minOccurs="1" maxOccurs="1"/> 
    <xs:element ref="born" minOccurs="1" maxOccurs="1"/> 
    <xs:element ref="dead" minOccurs="0" maxOccurs="1"/> 
     </xs:sequence> 
     <xs:attribute ref="id" use="optional"/> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="book" abstract="false" nillable="false"> 
    <xs:complexType mixed="false"> 
     <xs:sequence minOccurs="1" maxOccurs="1"> 
    <xs:element ref="isbn" minOccurs="1" maxOccurs="1"/> 
    <xs:element ref="title" minOccurs="1" maxOccurs="1"/> 
    <xs:element ref="author" minOccurs="0" 
    maxOccurs="unbounded"/> 
    <xs:element ref="character" minOccurs="0" 
    maxOccurs="unbounded"/> 
     </xs:sequence> 
     <xs:attribute ref="id" use="optional"/> 
     <xs:attribute ref="available" use="optional"/> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="character" abstract="false" nillable="false"> 
    <xs:complexType mixed="false"> 
     <xs:sequence minOccurs="1" maxOccurs="1"> 
    <xs:element ref="name" minOccurs="1" maxOccurs="1"/> 
    <xs:element ref="born" minOccurs="1" maxOccurs="1"/> 
    <xs:element ref="qualification" minOccurs="1" 
    maxOccurs="1"/> 
     </xs:sequence> 
     <xs:attribute ref="id" use="optional"/> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

XSDファイルを持っているシナリオでは

https://developer.marklogic.com/learn/2007-04-schema

、marklogicからチュートリアルを以下ました。

if (xdmp:database-name(xdmp:database()) ne 'Schemas') 
then error(
    QName('', 'NOT-SCHEMAS'), 'make sure the content-source is Schemas') 
else 
doc('tutorial.xsd') 
    /descendant::xs:element/@name 
(: results :) 

私は次のようになっています:以下の は私のXQueryで

name 
qualification 
born 
dead 
isbn 
title 
library 
author 
book 
character 

が、私は属性と一緒に値を必要とする

name xs:token 
qualification xs:token 
born xs:token 

は、誰かが私は、XQueryへの書き換えを助けることができますこれを上の出力にしますか?

答えて

6

要素をループして、それぞれの属性を取得するだけです。以下のreturn文では、それらを文字列に組み合わせたり、マップに配置したり、必要な処理を行うことができます。

if (xdmp:database-name(xdmp:database()) ne 'Schemas') then 
    error(
    QName('', 'NOT-SCHEMAS'), 'make sure the content-source is Schemas') 
else 
    for $element in doc('tutorial.xsd')/descendant::xs:element 
    where $element/@name and $element/@type 
    return $element/@name || " is a " || $element/@type 

where句には、name属性とtype属性の両方が存在する必要があります。どちらかを取得するにはorに変更するか、すべての名前が必要な場合は$element/@name、タイプが存在しない場合はイベントが必要です。

+0

ありがとうございました。あなたは私の一日を作った。私はXqueryを初めて使用しています。これを私に助けてくれてありがとう。 – happybayes

+0

慎重に結果を確認した後..iいくつか重複があることがわかります。私は空の名前と型を取得するように、ちょうど "is a"が表示されます。私はこれらの重複を取り除く方法を知りましたか?空の名前と属性を削除するために条件を使用したり書き込んだりすることができますか?あなたの助けを感謝@Dave Cassel – happybayes

+0

@mani where句を追加しました。 FLWORステートメントの詳細については、「XQueryおよびXSLTリファレンスガイド」の[FLWOR式](http://docs.marklogic.com/guide/xquery/langoverview#id_11626)セクションを参照してください。 –

1

@Daveカスは:

はthis.Itが働いた上で私を支援していただきありがとうございます。 私はまた、XQueryを修正しようとしました。おそらくあなたのコードは良いですが、自分のコードを共有していると感じました。

if (xdmp:database-name(xdmp:database()) ne 'Schemas') then 
    error(
    QName('', 'NOT-SCHEMAS'), 'make sure the content-source is Schemas') 
else 
    for $element in doc('tutorial.xsd')/descendant::xs:element 
     return if ($element/@name !="") then 
     $element/@name || "|" || $element/@type 
     else() 
(: results :) 

をご覧ください。

+0

私によく見えます –

関連する問題