Xml XQuery返回基于属性的元素

Xml XQuery返回基于属性的元素,xml,xquery,Xml,Xquery,我有一个XML文件,其中有4种主要类型的元素。产品、书籍、杂志和音乐 每本书、期刊和音乐CD都有一个属性IDREF,可以引用产品的属性ID 我试图实现的是找到每种产品的书籍、期刊或音乐CD数量,然后显示每种产品的产品ID和项目总数(书籍、期刊和音乐CD) 我的XML文件如下所示: <bookshop> <product ID="185.3.16"> <price currency="AU$">56.85</price>

我有一个XML文件,其中有4种主要类型的元素。产品、书籍、杂志和音乐

每本书、期刊和音乐CD都有一个属性
IDREF
,可以引用产品的属性
ID

我试图实现的是找到每种产品的书籍、期刊或音乐CD数量,然后显示每种产品的产品ID和项目总数(书籍、期刊和音乐CD)

我的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="123.45.67">
        <price currency="AU$">15.50</price>
        <comments>I love it</comments>
    </product>
    <product ID="134.41.33">
        <price currency="AU$">25.50</price>
        <comments></comments>
    </product>
    <product ID="214.56.82">
        <price currency="US$">35.65</price>
        <comments>Good</comments>
    </product>
    <product ID="263.18.37">
        <price currency="AU$">46.35</price>
        <comments>You should see this</comments>
    </product>
    <product ID="285.32.74">
        <price currency="US$">38.95</price>
        <comments></comments>
    </product>
    <product ID="321.12.76">
        <price currency="US$">15.95</price>
        <comments>Popular</comments>
    </product>
    <product ID="342.22.42">
        <price currency="US$">10.95</price>
        <comments>Sweet</comments>
    </product>
    <product ID="332.17.25">
        <price currency="US$">19.95</price>
        <comments></comments>
    </product>
    <book IDREF="185.3.16">
        <isbn>1-292-06118-9</isbn>
        <title>Database Systems</title>
        <authors>
            <author>
                <fname>Thomas</fname>
                <lname>Connolly</lname>
            </author>
            <author>
                <fname>Carolyn</fname>
                <lname>Begg</lname>
            </author>
        </authors>
        <publisher>Pearson Pty Ltd</publisher>
        <year>2015</year>
        <topic>Computer Science</topic>
        <description>This is the 6th edition. You can register online   to access the examples</description>    
        <keywords>
            <keyword>Database</keyword>
            <keyword>XML</keyword>
            <keyword>Distributed</keyword>
        </keywords>
    </book>
    <book IDREF="163.24.12">
        <isbn>1-123-456-810</isbn>
        <title>Core Java</title>
        <authors>
            <author>
                <fname>Horstmann</fname>
                <lname>Cornell</lname>
            </author>
        </authors>
        <publisher>PH Pty Ltd</publisher>
        <year>2012</year>
        <topic>Computer Science</topic>
        <description>It covers JAVA programming and JAVA script</description>
        <keywords>
            <keyword>JAVA</keyword>
            <keyword>XML</keyword>
            <keyword>Script</keyword>
        </keywords>
    </book>
    <book IDREF="123.45.67">
        <isbn>1-123-456-789</isbn>
        <title>Algorithms</title>
        <authors>
            <author>
                <fname>James</fname>
                <lname>Bond</lname>
            </author>
            <author>
                <fname>Harry</fname>
                <lname>Potter</lname>
            </author>
            <author>
                <fname>William</fname>
                <lname>Stallings</lname>
            </author>
        </authors>
        <publisher>Pearson Pty Ltd</publisher>
        <year>2013</year>
        <topic>Computer Science</topic>
        <description>It contains algorithms and their applications. You can download examples from the website</description>
    </book>
    <book IDREF="134.41.33">
        <isbn>1-213-431-770</isbn>
        <title>C++ Programming</title>
        <authors>
            <author>
                <fname>Larry</fname>
                <lname>Peterson</lname>
            </author>
        </authors>
        <publisher>Pearson Pty Ltd</publisher>
        <year>2010</year>
        <topic>Computer Science</topic>
        <description>C++ programming and its applications</description>
        <keywords>
            <keyword>C++</keyword>
            <keyword>Class</keyword>
            <keyword>Overloading</keyword>
            <keyword>Inheritance</keyword>
        </keywords>
    </book>
    <journal IDREF="214.56.82">
        <issn>S-11-123-123-456</issn>
        <title>Handyman</title>
        <volume>12</volume>
        <issue>23</issue>
    </journal>
    <journal IDREF="214.56.82">
        <issn>S-11-123-123-456</issn>
        <title>Handyman</title>
        <volume>12</volume>
        <issue>24</issue>
    </journal>
    <journal IDREF="214.56.82">
        <issn>S-11-123-123-456</issn>
        <title>Handyman</title>
        <volume>12</volume>
        <issue>25</issue>
    </journal>
    <journal IDREF="263.18.37">
        <issn>D-10-123-124-456</issn>
        <title>Information processing</title>
        <volume>5</volume>
        <issue>33</issue>
    </journal>
    <journal IDREF="263.18.37">
        <issn>D-10-123-124-456</issn>
        <title>Information processing</title>
        <volume>5</volume>
        <issue>34</issue>
    </journal>
    <journal IDREF="285.32.74">
        <issn>C-9-123-456-666</issn>
        <title>Mathmetics and Computing</title>
        <volume>17</volume>
        <issue>65</issue>
    </journal>
    <journal IDREF="285.32.74">
        <issn>C-9-123-456-666</issn>
        <title>Mathmetics and Computing</title>
        <volume>17</volume>
        <issue>66</issue>
    </journal>
    <musicCD IDREF="321.12.76">
        <title>Music</title>
        <producer>ABC Pty Ltd</producer>
        <year>2010</year>
        <category>Pop</category>
    </musicCD>
    <musicCD IDREF="342.22.42">
        <title>Stars</title>
        <producer>BBC Pty Ltd</producer>
        <year>2012</year>
        <category>Classic</category>
    </musicCD>
    <musicCD IDREF="332.17.25">
        <title>Wiggle Wiggle</title>
        <producer>Wiggle Pty Ltd</producer>
        <year>2005</year>
        <category>Kids</category>
    </musicCD>
</bookshop>

56.85
畅销书
28.6
15.50
我喜欢
25.50
35.65
好
46.35
你应该看看这个
38.95
15.95
流行的
10.95
甜蜜的
19.95
1-292-06118-9
数据库系统
托马斯
康诺利
卡罗琳
乞讨
皮尔森私人有限公司
2015
计算机科学
这是第六版。您可以在线注册以访问示例
数据库
XML
分布的
1-123-456-810
核心Java
霍斯特曼
康奈尔
PH私人有限公司
2012
计算机科学
它包括JAVA编程和JAVA脚本
JAVA
XML
剧本
1-123-456-789
算法
詹姆斯
债券
骚扰
陶工
威廉
种马
皮尔森私人有限公司
2013
计算机科学
它包含算法及其应用。您可以从网站下载示例
1-213-431-770
C++程序设计
拉里
彼得森
皮尔森私人有限公司
2010
计算机科学
C++程序设计及其应用
C++
等级
超载
遗产
S-11-123-123-456
勤杂工
12
23
S-11-123-123-456
勤杂工
12
24
S-11-123-123-456
勤杂工
12
25
D-10-123-124-456
信息处理
5.
33
D-10-123-124-456
信息处理
5.
34
C-9-123-456-666
数学与计算
17
65
C-9-123-456-666
数学与计算
17
66
音乐
ABC私人有限公司
2010
流行音乐
星星
英国广播公司私人有限公司
2012
经典
扭动
威格尔私人有限公司
2005
孩子们
我已经尝试过了,但是我不知道如何正确地遍历/匹配ID,或者如何进行计数

for $p in distinct-values(//product/@ID)
return
<result>
    {
        for $i in //journal[@IDREF = $p]
        return 
        <ID>{$p}</ID>
        <count></count>
    }
</result>
不同值(//product/@ID)中的$p的

返回
{
对于//journal[@IDREF=$p]中的$i
返回
{$p}
}
理想情况下,我的目标是让输出看起来像模拟数据:

<result>
    <ID>185.3.16</ID>
    <count>2</count>
<result>
<result>
    <ID>163.24.12</ID>
    <count>0</count>
<result>
...

185.3.16
2.
163.24.12
0
...

首先,您的查询有语法问题,因为
return
语句只需要一个结果(序列),所以您必须替换

    for $i in //journal[@IDREF = $p]
    return 
    <ID>{$p}</ID>
    <count></count>
如果底层XML模式正确地将id和idref属性定义为
@id
@idref
,或者您的实现无论如何都会拾取它们(例如,BaseX会),那么您也可以使用
fn:idref(…)
函数进行计数,这与上面使用的查询定义非常相似:

    <count>{ count(idref($product/@ID))}</count>
{count(idref($product/@ID))}

首先,您的查询有语法问题,因为
return
语句只需要一个结果(序列),所以您必须替换

    for $i in //journal[@IDREF = $p]
    return 
    <ID>{$p}</ID>
    <count></count>
如果底层XML模式正确地将id和idref属性定义为
@id
@idref
,或者您的实现无论如何都会拾取它们(例如,BaseX会),那么您也可以使用
fn:idref(…)
函数进行计数,这与上面使用的查询定义非常相似:

    <count>{ count(idref($product/@ID))}</count>
{count(idref($product/@ID))}

哪个版本的XQuery?哪个版本的XQuery?我现在明白了!谢谢你讲清楚了。老实说,我很迷茫,但你解释得很好。谢谢你:)我现在明白了!谢谢你讲清楚了。老实说,我很迷茫,但你解释得很好。谢谢你:)