0
votes

I'd like to get a list of attributes by a certain name, from a large number of XML-files in a certain folder.

With guidance from this question: XPath Function to query across 50 XML Files?), I'm trying to use XQuery for my file query as well, and installing the .Net version of Saxon I wrote a query like this:

Query.exe -qs:"collection('file:///Temp?select=*.xml')//group[1]/product[1]"

But from that I get everything below the element, including all child nodes. And I can't find a way to get just the attributes (or more specifically a certain "prd_id" attribute).

The XML looks like this (simplified version):

` <group>
    <product prd_id="2047">
        <articles>art</articles>
        <articles>art</articles>
        <articles>art</articles>
    </product>
</group>

I'd like the result from my XQuery search to be a list of prd_id attributes, or a list of pure product nodes with no child elements (the articles nodes are quite extensive in the real files, and would prevent me from picking out / copying the product attribute in question).

Just adding /@prd_id to the query results in an error like this:

First the query:
>Query.exe -qs:"collection('file:///Temp?select=*.xml')//group[1]/product[1]/@prd_id"

Error on line 1 of module with no systemId:XPTY0004: Cannot create an attribute node (prd_id) whose parent is a document node Query processing failed: Run-time errors were reported

Thanks, Andreas

1
I don't have saxon here right now, so I can't test it, but I would guess the error message tries to tell you that you can't serialize attributes. Using collection('file:///Temp?select=*.xml')//group[1]/product[1]/@prd_id/string() might already be enough.dirkk
Yes! You are right! Would you like to put it as an answer so that I could reward you by assigning it "Correct answer"? Please do! If you by chance would happen to know how to get an element written out without its subnodes, that would also be good knowledge (and the second choice alternative).Andreas Jansson

1 Answers

1
votes

As said in the comments, attributes can not be serialized. Hence, explicitly requesting the attribute string value should solve the problem:

collection('file:///Temp?select=*.xml')//group[1]/product[1]/@prd_id/string()

Regarding your second question, this is not possible using pure XPath/XQuery. You can not modify elements, this languages are for querying only (i.e. selecting certain nodes, grouping them, etc.pp.). You would need either XQuery Update to update element contents or XSLT to transform XML.