Page 1 of 1
need method to extract element with particular name-value pair
Posted: Sat Nov 10, 2012 1:08 am
by wscott44
I'm using <oXygen/> XML Editor 14.1 Enterprise. I'm working with an XML file we receive from a client. It contains ~5000 rows of product elements, each with 22 rows of properties.
The ones I need to focus on have this item in the list of 22: <prpty name="locationId" value="DLS"/>
There are only 47 such products with that value embedded. What I am doing now is, ftp the file to a Linux box and grep for the value, including a certain number of lines before it. Since the DLS string is near the end of the list,
grep -B50 "name=\"locationId\" value=\"DLS\"" filename.xml > DLSdata.xml
Yeah, it works, but it's tedious and imprecise. There MUST be a better way. I just need to be pointed in the best direction. Are there particular tools, options, or what?
Thanks,
Wayne
Re: need method to extract element with particular name-value pair
Posted: Wed Nov 14, 2012 10:41 pm
by wscott44
I figured it out with XQuery and I am quite pleased.
<DLS_RatePlans>
{
for $plan in doc("myfunky.xml")/catalog/prd, $prop in $plan/prpty
where ( (compare($prop/@name, "locationId") eq 0) and (compare($prop/@value, "DLS") eq 0) )
return
$plan
}
</DLS_RatePlans>
Re: need method to extract element with particular name-value pair
Posted: Wed Dec 12, 2012 1:49 am
by wscott44
How can I add a simple counter to be incremented every time it finds a match? XQuery was looking like a traditional programming language, until I started searching for solutions to this simple problem.
Re: need method to extract element with particular name-value pair
Posted: Wed Dec 12, 2012 10:31 am
by alex_jitianu
Hi,
Variables can't be updated. This means you can't write something like let $x := $x+1.
In XQuery you can simulate mutable variables using recursive functions. This means a function can pass in a modified value to the next function invocation in place of the value they already have. It more or less adds up to a mutable variable.
But I believe you could change your XQuery a bit in order to get what you need:
Code: Select all
<DLS_RatePlans>
{
let $plans :=
for $plan in doc("myfunky.xml")/catalog/prd, $prop in $plan/prpty
where ( (compare($prop/@name, "locationId") eq 0) and (compare($prop/@value, "DLS") eq 0) )
return
$plan
for $plan at $pos in $plans
return $plan
}
</DLS_RatePlans>
Variable $pos will give you the value of the counter you need for each 'plan'. If you just need the total counter you could just use count($plans)
Best regards,
Alex
Re: need method to extract element with particular name-value pair
Posted: Wed Dec 12, 2012 4:25 pm
by wscott44
Alex, thanks a billion. It's a tremendous help knowing what I can and cannot do.
I'll try your suggestion when I get to the office (on iPhone now).
Can you recommend a good technical reference that can help me gain better XML understanding? I have lots of comp science / IT experience, but many topics I'm just scratching the surface on.
Very grateful,
Wayne