【问题标题】:Xquery to get attribute name and type from a XSD schema?Xquery 从 XSD 模式获取属性名称和类型?
【发布时间】:2016-07-29 03:30:10
【问题描述】:

我正在学习 marklogic 的教程,

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

在一个场景中,我有 xsd 文件

    <!-- 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 中提取属性名称和类型。 以下是我的 XQuery:

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

我得到以下信息:

name
qualification
born
dead
isbn
title
library
author
book
character

但我需要属性和值一起

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

有人可以帮我重写 Xquery 以实现上述输出吗?

【问题讨论】:

    标签: xml xsd xquery marklogic marklogic-8


    【解决方案1】:

    您只需要遍历元素,然后获取每个元素的属性。在下面的 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 子句要求同时存在名称和类型属性。您可以将其更改为or 以获取其中任何一个,或者如果您想要所有名称,则只需要$element/@name,当类型不存在时的事件。

    【讨论】:

    • 非常感谢。你让我今天一整天都感觉很好。我是 Xquery 的新手。感谢您在这方面帮助我。
    • 仔细查看结果后..我看到有一些重复。就像我得到空的名称和类型一样,只是打印了“is a”。我想知道如何删除这些重复项?是否有我可以使用或编写条件来删除空名称和属性的 ia distict 函数?感谢您的帮助@Dave Cassel
    • @mani 我添加了 where 子句。您可以在 XQuery 和 XSLT 参考指南的FLWOR Expressions 部分查看更多关于 FLWOR 语句的信息。
    • 非常感谢,请查看我的回答。但我猜你的代码是干净的。
    • 我认为你可以帮助我解决这个问题? stackoverflow.com/questions/36510984/…如果您对此有任何建议,请告诉我。
    【解决方案2】:

    @戴夫卡斯:

    感谢您帮助我解决这个问题。它奏效了。 我还尝试修改 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 :)
    

    请查看。

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 1970-01-01
      • 2010-09-21
      • 2023-02-02
      • 1970-01-01
      • 2020-03-17
      • 2012-04-12
      • 2010-09-14
      相关资源
      最近更新 更多