XPath : finding an attribute node (and only one)
        Posted  
        
            by subtenante
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by subtenante
        
        
        
        Published on 2009-07-09T07:21:26Z
        Indexed on 
            2010/03/15
            11:29 UTC
        
        
        Read the original article
        Hit count: 239
        
xpath
What is the XPath to find only ONE node (whichever) having a certain attribute (actually I'm interested in the attribute, not the node). For example, in my XML, I have several tags having a lang attribute. I know all of them must have the same value. I just want to get any of them.
Right now, I do this : //*[1][@lang]/@lang, but it seems not to work properly, for an unknown reason.
My tries have led me to things ranging from concatenation of all the @lang values ('en en en en...') to nothing, with sometimes inbetween what I want but not on all XML.
EDIT :
Actually //@lang[1] can not work, because the function position() is called before the test on a lang attribute presence. So it always takes the very first element found in the XML. It worked best at the time because many many times, the lang attribute was on root element.
© Stack Overflow or respective owner