need method to extract element with particular name-value pair

Questions about XML that are not covered by the other forums should go here.
wscott44
Posts: 7

need method to extract element with particular name-value pair

Sat Nov 10, 2012 1:08 am

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
wscott44
Posts: 7

Re: need method to extract element with particular name-value pair

Wed Nov 14, 2012 10:41 pm

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>
wscott44
Posts: 7

Re: need method to extract element with particular name-value pair

Wed Dec 12, 2012 1:49 am

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.
alex_jitianu
Posts: 645

Re: need method to extract element with particular name-value pair

Wed Dec 12, 2012 10:31 am

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
wscott44
Posts: 7

Re: need method to extract element with particular name-value pair

Wed Dec 12, 2012 4:25 pm

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

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 1 guest