-1

I am trying to return a certain set of values however the query is not quite returning what I would like. I would like to return records by the author "Hennie J. Steenhagen" grouped by year. However what it is returning is records grouped by year if it’s of the same year as one of Hennies records. Not only Hennies.

For example, if we have the record <www><author>Hennie*</author><year>1990</year></www> and <www><author>Derpie</author><year>1990></year></www> the query will return both records grouped in the year 1990, I would only like Hennies to be returned.

for $y in /*/*/year where $y/../author ="Hennie J. Steenhagen" return <year-Pub>{$y}{/*/*[year = $y]}</year-Pub>

2 Answers 2

2

Your question is quite difficult to understand because your XPath addresses a larger XML node tree than the example XML you have provided. However for the example I will assume that your records are named record. Also your output of your XPath does not make a lot of sense to me, but I will assume that you know what you want!

Given the XML:

<record>
    <www>
        <author>Hennie J. Steenhagen</author>
        <year>1990</year>
    </www> 
    and 
    <www>
        <author>Derpie</author>
        <year>1990></year>
    </www>
</record>

If you have an XQuery 3.0 processor, you could use the following:

/record/www[author = "Hennie J. Steenhagen"] ! <year-Pub>{year}{.}</year-Pub>

If you only have access to an XQuery 1.0 processor, then you could fall-back to the following:

for $w in /record/www[author = "Hennie J. Steenhagen"]
return
    <year-Pub>{$w/year}{$w}</year-Pub>

Both of my examples only use a single predicate which will only filter the data once. Whereas your self-found solution uses both a predicate and a where expression, and so has to filter the data twice.

0

Fixed it,

for $y in /*/*/year where $y/../author ="Hennie J. Steenhagen" and /*/*[year=$y] return <year-Pub>{$y/../*}</year-Pub>

Thanks for any one whom spend their time looking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.