You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Enric Jaen <en...@nmg.upc.es> on 2006/01/18 12:23:12 UTC

how to filter elements in XPath

Hi,
Is there any way to filter the child elements of a given Xpath expression? (without using multiple paths)

For example, having this:

<AAA>
  <BBB/>
  <CCC/>
  <DDD/>
</AAA>

I'd like to return AAA with only BBB and CCC elements, like this:

<AAA>
  <BBB/>
  <CCC/>
</AAA>


Regards,
-Enric


Re: how to filter elements in XPath

Posted by da...@us.ibm.com.
Enric Jaen writes:
For example, having this:
<AAA>
  <BBB/>
  <CCC/>
  <DDD/>
</AAA>
I'd like to return AAA with only BBB and CCC elements...

But what is the criterion that designated BBB and CCC?
You could go by name:
select="AAA|AAA/*[name()='BBB' or name()='CCC']"
You could go by position:
select="AAA|AAA/*[position() &lt; 3]"
Or if BBB and CCC had a certain attribute, you could test for it.

In any case, this is a generic XPath question, so you could ask it on
xsl-list or in comp.text.xml and get more answers.
.................David Marston

Re: xpath question

Posted by Mukul Gandhi <ga...@gmail.com>.
You can write as

../line[invoicenum = current()/invoicenum]

Regards,
Mukul

On 1/18/06, Anamitra.Bhattacharyya@mro.com
<An...@mro.com> wrote:
> Hi All
> I have an xml which looks something like this below. I am currently in the
> invoice node and I want to select all the line nodes that have the same
> invoicenum as the invoice node that I am in. It will be almost like this:
>
> ../line[invoicenum= <the current invoice elements invoicenum>]
>
> I am not sure how to express that <the current invoice elements invoicenum>
> in this xpath expr. Can any one suggest something.
>
> <Root>
>    <invoice>
>        <invoicenum>aaaa</invoicenum>
>    </invoice>
>    <invoice>
>        <invoicenum>bbbb</invoicenum>
>    </invoice>
>    <line>
>         <desc>line1 for aaaa</desc>
>         <invoicenum>aaaa</invoicenum>
>    </line>
>    <line>
>         <desc>line2 for aaaa</desc>
>         <invoicenum>aaaa</invoicenum>
>    </line>
>    <line>
>         <desc>line1 for bbbb</desc>
>         <invoicenum>bbbb</invoicenum>
>    </line>
> </Root>
>
> thanks
> Anamitra

xpath question

Posted by An...@mro.com.
Hi All
I have an xml which looks something like this below. I am currently in the
invoice node and I want to select all the line nodes that have the same
invoicenum as the invoice node that I am in. It will be almost like this:

../line[invoicenum= <the current invoice elements invoicenum>]

I am not sure how to express that <the current invoice elements invoicenum>
in this xpath expr. Can any one suggest something.

<Root>
    <invoice>
        <invoicenum>aaaa</invoicenum>
    </invoice>
    <invoice>
        <invoicenum>bbbb</invoicenum>
    </invoice>
    <line>
         <desc>line1 for aaaa</desc>
         <invoicenum>aaaa</invoicenum>
    </line>
    <line>
         <desc>line2 for aaaa</desc>
         <invoicenum>aaaa</invoicenum>
    </line>
    <line>
         <desc>line1 for bbbb</desc>
         <invoicenum>bbbb</invoicenum>
    </line>
</Root>

thanks
Anamitra


Re: how to filter elements in XPath

Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hi, Enric.

Enric Jaen <en...@nmg.upc.es> wrote on 2006-01-18 06:23:12 AM:
> Is there any way to filter the child elements of a given Xpath 
> expression? (without using multiple paths)
> 
> For example, having this:
> 
> <AAA>
>   <BBB/>
>   <CCC/>
>   <DDD/>
> </AAA>
> 
> I'd like to return AAA with only BBB and CCC elements

     If I understand correctly, you want a single XPath expression that 
will return the element AAA with its child element DDD "hidden."  Did I 
understand you correctly?  That can't be done in XPath - if the AAA node 
is returned by an expression, you have access to all its children.

     Perhaps an XMLFilter would satisfy your requirement.  See [1] for 
more information.

Thanks,

Henry
[1] http://xml.apache.org/xalan-j/apidocs/org/xml/sax/XMLFilter.html
------------------------------------------------------------------
Henry Zongaro      Xalan development
IBM SWS Toronto Lab   T/L 969-6044;  Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com


RE: how to filter elements in XPath

Posted by Florent Georges <da...@yahoo.fr>.
Enric Jaen wrote:

> For example, having this:

> <AAA>
>   <BBB/>
>   <CCC/>
>   <DDD/>
> </AAA>

> I'd like to return AAA with only BBB and CCC elements, like this:

> <AAA>
>   <BBB/>
>   <CCC/>
> </AAA>

  XPath can select nodes, not modify them.  So if you want to select
AAA, you can't select a modified AAA.  But you can choose to select the
sequence (BBB, CCC) instead of (BBB, CCC, DDD).

--drkm




















	

	
		
___________________________________________________________________________ 
Nouveau : téléphonez moins cher avec Yahoo! Messenger ! Découvez les tarifs exceptionnels pour appeler la France et l'international.
Téléchargez sur http://fr.messenger.yahoo.com