簡體   English   中英

XQuery查找元素,其中匹配屬性的子級為空

[英]XQuery find element where child of a matching attribute is empty

我有一個XML文件,我試圖從中提取元素書籍和期刊的標題,其中按ID和IDREF匹配的產品在注釋中沒有值。

因此,我擁有的XML文件如下所示:

<bookshop>
    <product ID="185.3.16">
        <price currency="AU$">56.85</price>
        <comments>Best sell</comments>
    </product>
    <product ID="163.24.12">
        <price currency="NZ$">28.6</price>
        <comments />
    </product>
    <product ID="332.17.25">
        <price currency="US$">19.95</price>
        <comments></comments>
    </product>
    <book IDREF="163.24.12">
        <title>Core Java</title>
    </book>
    <book IDREF="185.3.16">
        <title>C++ Development</title>
    </book>
    <journal IDREF="332.17.25">
        <title>Mathematics and Computing</title>
    </journal>
</bookshop>

我想要達到的輸出是:

<title>Core Java</title>
<title>Mathematics and Computing</title>

因為這本書的C ++開發的IDREF與子注釋沒有價值的產品的ID相匹配。

我試圖通過使用xquery創建腳本來嘗試通過查找具有唯一ID屬性的所有元素產品來創建腳本,其中子注釋也為空,但是在嘗試查找沒有注釋的產品時都遇到麻煩,然后將這些結果轉換為我可以返回的結果。 (在解決產品之前,我不能真正給予退貨,因此我無法為此留出很多余地。)但是,到目前為止,我已經嘗試了以下方法:

for $id in distinct-values(//product/@ID)
where count(//product/comments/text()) > 0
return ...?

任何幫助將非常感激。

提前致謝!

“我正在嘗試提取元素書籍和期刊的標題,其中通過ID和IDREF匹配的產品在注釋中沒有價值”

這是返回您所描述問題的一種可能的XPath:

/bookshop/*[@IDREF = /bookshop/product[normalize-space(comments) = '']/@ID]/title

如果您只想選擇bookjournal ,只需添加一個謂詞: [self::book | self::journal] [self::book | self::journal]/*

xpathtester demo

輸出:

<title>Core Java</title>

<title>Mathematics and Computing</title>

更新:

局限性尚不清楚(不能使用哪種XPath表達式)。 假設您不允許使用XPath謂詞( [] ),則可以將其替換為where子句,如下所示:

declare variable emptyProductIDs := /bookshop/product[not(comments/text())]/@ID

for $element in /bookshop/*
where $element/@IDREF = $emptyProductIDs
return $element/title

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM