473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

XPath or XSL Transform Help

I am new to XML Programming and I am hoping someone can help me out
with this.

Assuming I have the following XML:

<root>
<E C="P_E1" O="E1" Y='True'>
<A C="P_E1A1" V="7" Y='True'/>
<A C="P_E1A2" V="8" />
</E>
<E C="P_E2" O="E1">
<A C="P_E1A1" V="7" />
<A C="P_E1A2" V="8" />
</E>
<E C="P_E3" O="3" Y='True'>
<A C="P_E3A1" V="15" Y='True'/>
<E C="P_E3E1" O="E1" Y='True'>
<A C="P_E3E1A1" V="15" Y='True'/>
<A C="P_E3E1A2" V="16" />
<A C="P_E3E1A3" V="17" />
<A C="P_E3E1A4" V="18" />
<E C="P_E3E1E1" O="E1" Y='True'>
<A C="P_E3E1E1A1" V="15" Y='True'/>
</E>
</E>
</E>
<E C="P_E4" O="4" >
<A C="P_E4A1" V="19" />
<A C="P_E4A2" V="20" />
<E C="P_E4E1" O="E1" Y='True'>
<A C="P_E4E1A1" V="15" Y='True'/>
</E>
</E>
</root>

Basically, I want an xPath that will select all of the top most E
elements where Y='True'.
When I write top most, I mean only the followng:
- the parent, not children
- single node, if there are no children

So, for the example above, the following nodes would be selected:
- <E C="P_E1" O="E1" Y='True'>
- <E C="P_E3" O="3" Y='True'>
- <E C="P_E4E1" O="E1" Y='True'>

I assume this can be done; I just cannot get the syntax down.

Here is what I have so far: //E [@Y='True']. However, this selects all
of the nodes including the children.

If this helps, the Y attribute will either be equal to True on not
exist. As shown in the example above.

After those nodes are selected, I need to transform the nodes using
XSLT. I am mentioning this just in case the xPath cannot be written
and XSLT needs to be used to select the nodes. Once the nodes are
selected, I think I can take it from there.

Thanks for your help.

Regards,

R

Dec 11 '06 #1
2 1377
Candle wrote:
Basically, I want an xPath that will select all of the top most E
elements where Y='True'.
Your only difficulty here is making "top most" something that's
sufficiently explicit that you can express it in XSLT.

One way to say this: You want all E elements whose Y attribute is
"True", but whose parent's Y attribute is either absent or not "True".

//E[@Y="True" and not(../@Y="True")]

It's probably more efficient to rephrase this as: You want all E
elements whose Y attribute is "True", that are children of an element
whose Y attribute is either absent or not "True".

//*[not(@Y="True")]/E[@Y="True"]

If you know the parent is always going to be an E, it's probably even
more efficient to say:

//E[not(@Y="True")]/E[@Y="True"]

This is assuming that once Y becomes True, it is True for all descendant
E's below that point ... otherwise, this simple parent/child test
applied to all available E's won't work, and you'll need a more explicit
description of what "top most" means.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 11 '06 #2
Joseph,

Worked like a charm.

Thx,

R

Joseph Kesselman wrote:
Candle wrote:
Basically, I want an xPath that will select all of the top most E
elements where Y='True'.

Your only difficulty here is making "top most" something that's
sufficiently explicit that you can express it in XSLT.

One way to say this: You want all E elements whose Y attribute is
"True", but whose parent's Y attribute is either absent or not "True".

//E[@Y="True" and not(../@Y="True")]

It's probably more efficient to rephrase this as: You want all E
elements whose Y attribute is "True", that are children of an element
whose Y attribute is either absent or not "True".

//*[not(@Y="True")]/E[@Y="True"]

If you know the parent is always going to be an E, it's probably even
more efficient to say:

//E[not(@Y="True")]/E[@Y="True"]

This is assuming that once Y becomes True, it is True for all descendant
E's below that point ... otherwise, this simple parent/child test
applied to all available E's won't work, and you'll need a more explicit
description of what "top most" means.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: K.Strunk | last post by:
Hi! I have a problem signing a XML-document. I need to sign a subnode of a document. So I need to refer to this subnode from within my signature. But how can I do that with XPath? I tried the...
6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
3
by: Tjerk Wolterink | last post by:
Hello i have xml code like this: <page:page xmlns:page="namespacefor page"> <page:section> <page:header> <b>Hello</b>There </page:header> <page:content> --- HTML CODE like: <i>Y</i>es i...
4
by: Pat Turner | last post by:
Hi, I have some XML like this: <family> <person name="bob"> <father ref="../../person" /> </person> <person name="charlie"> <child ref="../../person" />
4
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a...
1
by: Sonu Kapoor | last post by:
Hi, I would like to use xpath with xslt. I know that this should be possible, but I dont get it ! Here is what I have tried so far: ==================================== my xml file:...
2
by: Pawel | last post by:
I have small problem with XslTransformation. I get from WebService xml document. I have xslt and I want transform xml document to html code. It's look easy but I cant't manage with xPath. Maybe...
2
by: Chucker | last post by:
Hi folks, I would like to select some nodes from a very large XML document and then apply a stylesheet to the dom fragment / nodelist that is the result of my XPath-Query. I used to use MSXML...
2
by: Monty | last post by:
Despite reading posts in Google, I don't understand XPATH. Can someone help me write an XPATH. From Google I think my problem is that the default namespace does not have a prefix. I can't change...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.