944,118 Members | Top Members by Rank

Ad:
Oct 22nd, 2009
0

XPATH Question on Filtering / Condition

Expand Post »
I have an xml like below
<PO>
<ITEM>
<ID>1000</ID>
<PRICE>10</PRICE>
</ITEM>
<ITEM>
<ID>1000</ID>
<PRICE>10</PRICE>
</ITEM>
<ITEM>
<ID>1001</ID>
<PRICE>10</PRICE>
</ITEM>
<ITEM>
<ID>1001</ID>
<PRICE>10</PRICE>
</ITEM>
</PO>

I want an xpath that will get only ID = 1000 and the output should be like below
<PO>
<ITEM>
<ID>1000</ID>
<PRICE>10</PRICE>
</ITEM>
<ITEM>
<ID>1000</ID>
<PRICE>10</PRICE>
</ITEM>
</PO>

I tried /PO/ITEM[ID="1000"] but it gives the following but I need even the root "PO" also...

<ITEM>
<ID>1000</ID>
<PRICE>10</PRICE>
</ITEM>
<ITEM>
<ID>1000</ID>
<PRICE>10</PRICE>
</ITEM>

Thanks for the help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hangon is offline Offline
4 posts
since Oct 2009
Oct 31st, 2009
0
Re: XPATH Question on Filtering / Condition
As far as I know you cannot use an XPath expression to do what you want to do.

Here is how I get the result you are looking for
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3.  
  4. <xsl:output method="xml" indent="yes" />
  5.  
  6. <xsl:template match="/" >
  7. <xsl:element name="PO">
  8. <xsl:copy-of select="//ITEM[ID='1000']" />
  9. </xsl:element>
  10. </xsl:template>
  11.  
  12. </xsl:stylesheet>
Reputation Points: 22
Solved Threads: 11
Junior Poster
fpmurphy is offline Offline
144 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in XML, XSLT and XPATH Forum Timeline: Checking the date
Next Thread in XML, XSLT and XPATH Forum Timeline: Nesting of xsl:key() within xsl:key() & combining results of xsl:key()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC