XQuery:如果属性包含另一个特殊属性,则返回该属性的值

XQuery:如果属性包含另一个特殊属性,则返回该属性的值,xquery,Xquery,我想返回“field”中包含“databases”属性的所有书籍的结果 .xml文件的示例: <?xml version="1.0" encoding="UTF-?> <testbook> <book> <author> AuthorGuy </author> <title> theBook</title> <field> databases </field> </book>

我想返回“field”中包含“databases”属性的所有书籍的结果

.xml
文件的示例:

<?xml version="1.0" encoding="UTF-?>
<testbook>
<book>
<author> AuthorGuy </author>
<title> theBook</title>
<field> databases </field>
</book>
</testbook>
有两个问题:

  • 元素包含在
    元素中,并且不是
    的直接子元素,请修复路径表达式
  • 标记包含用空格包装的字段,请使用
    包含($string,$needle)
    而不是简单的比较
  • 一个有效的例子:

    let $document := document{<testbook>
    <book>
    <author> AuthorGuy </author>
    <title> theBook</title>
    <field> databases </field>
    </book>
    </testbook>}
    
    for $e in $document//testbook[contains(book/field, "databases")]
    return $e/book/title
    
    let$document:=document{
    作者
    书
    数据库
    }
    对于$document//testbook[contains(book/field,“databases”)中的$e
    返回$e/书籍/标题
    
    我没有意识到中的空格会导致问题,我将来会意识到这一点,但是我似乎无法让您的工作示例正常工作。我不确定这是因为我的无知还是因为我的XML编辑器不是我的朋友。另外,请你详细说明你所说的,我对术语不太熟悉。简单比较的想法来自一个讲师的powerpoint幻灯片,其中有一个类似的例子,它与his.xml“for$e in//employee[sex=“F”]/name return$e/lname”非常相似。这个回答有点长。不管怎样,我真的很感谢你的输入/帮助,如果你还有更多需要纠正的地方,我会更进一步:-)例如,
    contains(“hello world”,“world”)
    将返回true,因为“haystack”
    hello world
    包含“针”
    world
    。空白是否重要通常取决于配置。如果您不能提供示例(和示例输入!),则很难提供关于为什么它对您的讲师有效的更多细节。
    let $document := document{<testbook>
    <book>
    <author> AuthorGuy </author>
    <title> theBook</title>
    <field> databases </field>
    </book>
    </testbook>}
    
    for $e in $document//testbook[contains(book/field, "databases")]
    return $e/book/title