XQuery不会生成请求的数据

时间:2018-02-06 19:35:11

标签: xml xpath xquery

我正在为一个学习XML的项目工作,这是我第一次使用XQuery。我甚至无法处理一个命令所以我在这里问为什么它不起作用。我发布了我的XML代码和我的XQuery,你们可以看看,问题是什么。

XML:

    <?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sharam_etemadi_1421685_vodafone.xsl"?>               
<Vodafone>
    <Customer Customer_ID="10000">
        <Gender>Male</Gender>
        <LastName>Meier</LastName>
        <FirstName>Olaf</FirstName>
        <Username>Oleier</Username>
        <Password>Oleier123</Password>
        <Email>Olaf@meier.com</Email>
        <PhoneNumber>0511 654321</PhoneNumber>
        <Country>Germany</Country>
        <Contract>
            <Contracted> 
                <Day>21</Day>
                <Month>12</Month>
                <Year>2017</Year>
            </Contracted>
            <Expiration> 
                <Day>21</Day>
                <Month>12</Month>
                <Year>2019</Year>
            </Expiration>
            <CreditCard CardNumber="1234 5678 9012 3457" SecurityCode="260">
                <Brand>Visa</Brand>
                <Expiration>
                    <Month>08</Month>
                    <Year>2022</Year>
                </Expiration>
            </CreditCard>
            <MobilePhoneNumber>0173 7654321</MobilePhoneNumber>
            <Tariff>Young XL</Tariff>
        </Contract>
    </Customer>
    <Customer Customer_ID="10001">
        <Gender>Female</Gender>
        <LastName>Harman</LastName>
        <FirstName>Agathe</FirstName>
        <Username>Agathe1337</Username>
        <Password>hArAtHe77</Password>
        <Email>Agathe@gmx.de</Email>
        <PhoneNumber>0511 123456</PhoneNumber>
        <Country>Germany</Country>
        <Contract>
            <Contracted> 
                <Day>01</Day>
                <Month>02</Month>
                <Year>2017</Year>
            </Contracted>
            <Expiration> 
                <Day>01</Day>
                <Month>02</Month>
                <Year>2019</Year>
            </Expiration>
            <CreditCard CardNumber="4321 8765 4321 8795" SecurityCode="062">
                <Brand>MasterCard</Brand>
                <Expiration>
                    <Month>02</Month>
                    <Year>2022</Year>
                </Expiration>
            </CreditCard>
            <MobilePhoneNumber>0173 1234567</MobilePhoneNumber>
            <Tariff>Young L</Tariff>
        </Contract>
    </Customer>
    <Customer Customer_ID="10002">
        <Gender>Male</Gender>
        <LastName>Müller</LastName>
        <FirstName>Detlef</FirstName>
        <Username>Detti88</Username>
        <Password>password123</Password>
        <Email>Detlef@web.de</Email>
        <PhoneNumber>0511 687642</PhoneNumber>
        <Country>Germany</Country>
        <Contract>
            <Contracted> 
                <Day>15</Day>
                <Month>05</Month>
                <Year>2016</Year>
            </Contracted>
            <Expiration> 
                <Day>15</Day>
                <Month>05</Month>
                <Year>2018</Year>
            </Expiration>
            <CreditCard CardNumber="2431 5942 6482 1379" SecurityCode="555">
                <Brand>AmericanExpress</Brand>
                <Expiration>
                    <Month>05</Month>
                    <Year>2025</Year>
                </Expiration>
            </CreditCard>
            <MobilePhoneNumber>0172 7641359</MobilePhoneNumber>
            <Tariff>Young M</Tariff>
        </Contract>
    </Customer>
    <Customer Customer_ID="10003">
        <Gender>Female</Gender>
        <LastName>Basel</LastName>
        <FirstName>Annabelle</FirstName>
        <Username>Ansel89</Username>
        <Password>safetyfirst5</Password>
        <Email>basel@strato.de</Email>
        <PhoneNumber>0512 555987</PhoneNumber>
        <Country>Germany</Country>
        <Contract>
            <Contracted> 
                <Day>27</Day>
                <Month>12</Month>
                <Year>2017</Year>
            </Contracted>
            <Expiration> 
                <Day>27</Day>
                <Month>12</Month>
                <Year>2019</Year>
            </Expiration>
            <CreditCard CardNumber="1111 2222 3333 4444" SecurityCode="123">
                <Brand>MasterCard</Brand>
                <Expiration>
                    <Month>12</Month>
                    <Year>2020</Year>
                </Expiration>
            </CreditCard>
            <MobilePhoneNumber>0173 7775556</MobilePhoneNumber>
            <Tariff>Young S</Tariff>
        </Contract>
    </Customer>
</Vodafone>

我想要开始的第一个XQuery是:向我展示合约在2020年之前结束的所有姓氏(到期)。所以我尝试了这段代码:for $x in /Vodafone/Customer/Contract/Expiration where $x/Year<2020 return $x/FirstName

为什么这不起作用,我的错误在哪里?

我还有4个我想要使用的XQuery:

  1. 告诉我所有来自德国的客户
  2. 告诉我所有客户,电话号码从0511开始
  3. 告诉我所有关税是年轻人的客户
  4. 告诉我所有信用卡品牌为Visa的客户
  5. 也许有人可以帮助我获得有关XQuery的更多知识?

    谢谢!

2 个答案:

答案 0 :(得分:3)

错误发生在return语句中:'FirstName'节点不是'Expiration'的子节点,因此正确的xquery将如下:

for $x in /Vodafone/Customer
where $x/Contract/Expiration/Year<2020
return $x/FirstName

但是,您可以使用如下所示的简单xpath实现此查询:

/Vodafone/Customer[Contract/Expiration/Year < 2020]/FirstName

此外,所有其他四个查询都很简单,可以通过上面的xpath表达式实现。我建议看看xpath和xquery基本教程,例如w3c:xpathxquery

答案 1 :(得分:1)

您感兴趣的客户不是他们的合同到期日期,所以请仔细阅读。

for $customer in /Vodafone/Customer
where $customer/Contract/Expiration/Year < 2020
return $customer

所有其他查询只是where过滤器的变体。