Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

XPath help. Pretty basic

Options
  • 08-06-2010 3:57am
    #1
    Registered Users Posts: 729 ✭✭✭


    I want to select the first product in the following XML with name 'bread'

    At first I was thinking that this would work:
    /SHOP/BASKET/PRODUCT[NAME='bread'][1]
    
    However, this selects the first bread product for each basket

    I only want to select the first bread product that appears in the document. I can return this by using:
    /SHOP/BASKET[PRODUCT/NAME='bread'][1]/PRODUCT[NAME='bread'][1]
    

    Fair enough but I imagine there is a more succinct solution. Much obliged if anyone can help.

    Cheers

    EDIT: This also works, though I believe the above method is more efficient:
    /SHOP/descendant::PRODUCT[NAME='bread'][1]
    
    Anyone know a more efficent method?
    <?xml version="1.0" encoding="UTF-8"?>
    <SHOP>
    	<BASKET colour="black">
    		<PRODUCT>
    			<NAME>beans</NAME>
    			<VAL>BAKED</VAL>
    		</PRODUCT>
    	</BASKET>
    	<BASKET colour="green">
    		<PRODUCT>
    			<NAME>bread</NAME>
    			<VAL>BROWN</VAL>
    		</PRODUCT>
    		<PRODUCT>
    			<NAME>bread</NAME>
    			<VAL>WHITE</VAL>
    		</PRODUCT>
    	</BASKET>
    	<BASKET colour="blue">
    		<PRODUCT>
    			<NAME>pasta</NAME>
    			<VAL>MAC</VAL>
    		</PRODUCT>
    		<PRODUCT>
    			<NAME>bread</NAME>
    			<VAL>TOASTED</VAL>
    		</PRODUCT>
    	</BASKET>
    </SHOP>
    
    


Comments

  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    You have a different xpath parser than me I think, but in your solution:
    /SHOP/BASKET[PRODUCT/NAME='bread'][1]/PRODUCT[NAME='bread'][1]
    

    does it work without repeating the predicate?
    /SHOP/BASKET[PRODUCT/NAME='bread'][1]/PRODUCT
    


  • Registered Users Posts: 729 ✭✭✭spectre


    does it work without repeating the predicate?
    /SHOP/BASKET[PRODUCT/NAME='bread'][1]/PRODUCT
    

    No, this will retrieve every product in the correct basket. We want to retrieve only the bread product.

    I'm pretty sure that XPath results will be independent of the parser used. I'm developing my app in XMLSpy using the default integrated parser fwiw


  • Registered Users Posts: 24 xonet


    <?xml version="1.0"?>
    
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    
      <xsl:output method="xml" />
    
      <xsl:template match="/">
         <xsl:value-of select="//PRODUCT[NAME='bread'][1]" />
      </xsl:template>
    
    </xsl:stylesheet>
    


  • Registered Users Posts: 729 ✭✭✭spectre


    Thanks xonet, this does indeed yield the correct answer.

    Despite the fact that we are passing two nodes to the xsl:value-of command, it seems to only process the first. My existing code uses this facility in several places but I'm not totally happy with it.

    Surely it is considered bad practice to pass an XPath expression which yields a multi node set? This is the reason I was looking for an efficent XPath expression which would yield only the desired first node.


Advertisement