Reputation:
I'm working right now for a project to learn XML and it's the first time I'm working with XQuery's. I can't even handle one command so I'm asking here why it's not working. I post my XML Code and my XQuery, that you guys can look, what the problem is.
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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</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>
The first XQuery I want to start with is: Show me all Lastname's where the contract's end before 2020 (Expiration). So I tried this code: for $x in /Vodafone/Customer/Contract/Expiration
where $x/Year<2020
return $x/FirstName
Why this is not working, where is my error?
And I got 4 more XQuery's I want to work with:
Maybe someone can help me out to get more knowledge about XQuery?
Thanks!
Upvotes: 0
Views: 85
Reputation: 1993
Your interested Customers not their contract expiration dates so iterate through them.
for $customer in /Vodafone/Customer
where $customer/Contract/Expiration/Year < 2020
return $customer
All your other queries are simply variations on the where filter.
Upvotes: 1
Reputation: 197
The error is on the return statement: 'FirstName' node is not a child of 'Expiration' so the correct xquery would be the following:
for $x in /Vodafone/Customer
where $x/Contract/Expiration/Year<2020
return $x/FirstName
However, you can achieve this query with a simple xpath like the below:
/Vodafone/Customer[Contract/Expiration/Year < 2020]/FirstName
Also, all the other four queries are simple can be achieved with xpath expressions like the above. I would suggest to have a look at xpath and xquery basic tutorials like the w3c ones here: xpath and xquery
Upvotes: 3