need method to extract element with particular name-value pair
Questions about XML that are not covered by the other forums should go here.
-
- Posts: 7
- Joined: Tue Jan 05, 2010 7:58 pm
need method to extract element with particular name-value pair
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
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
-
- Posts: 7
- Joined: Tue Jan 05, 2010 7:58 pm
Re: need method to extract element with particular name-value pair
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>
<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>
-
- Posts: 7
- Joined: Tue Jan 05, 2010 7:58 pm
Re: need method to extract element with particular name-value pair
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.
-
- Posts: 1016
- Joined: Wed Nov 16, 2005 11:11 am
Re: need method to extract element with particular name-value pair
Post 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:
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
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>
Best regards,
Alex
-
- Posts: 7
- Joined: Tue Jan 05, 2010 7:58 pm
Re: need method to extract element with particular name-value pair
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
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”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service