<Articles>

    <Article ID="111">
        <author>Peter Paul</author>
        <pubDate>01/01/2015</pubDate>  
        <Translations>
            <lang1>English</lang1>
            <lang2>French</lang2>
            <lang3>Arab</lang3>
            <lang3>Chinese</lang3>
        </Translations>
    </Article>

    <Article ID="222">
        <author>Monkey Rice</author>
        <pubDate>01/01/2016</pubDate>  
        <Translations>
            <lang1>English</lang1> 
        </Translations>
    </Article>

    <Article ID="333">
        <author>John Silas</author>
        <pubDate>01/01/2017</pubDate>  
        <Translations>
            <lang1 country="england">English</lang1>
            <lang2 country="france">French</lang2> 
            <lang3 country="china">Chinese</lang3>
        </Translations>
    </Article>

</Articles>

I want to select any attribute in the entire document (no matter the dept) that maches country=england, only with xpath. am currectly using:

//*[@country = england]

and its only returning an invalid xpath error. any quick answer please ?

Hi use like below. i tested it. hope this helps you.

<?php 
'<Articles>

    <Article ID="111">
        <author>Peter Paul</author>
        <pubDate>01/01/2015</pubDate>  
        <Translations>
            <lang1>English</lang1>
            <lang2>French</lang2>
            <lang3>Arab</lang3>
            <lang3>Chinese</lang3>
        </Translations>
    </Article>

    <Article ID="222">
        <author>Monkey Rice</author>
        <pubDate>01/01/2016</pubDate>  
        <Translations>
            <lang1>English</lang1> 
        </Translations>
    </Article>

    <Article ID="333">
        <author>John Silas</author>
        <pubDate>01/01/2017</pubDate>  
        <Translations>
            <lang1 country="england">English</lang1>
            <lang2 country="france">French</lang2> 
            <lang3 country="china">Chinese</lang3>
        </Translations>
    </Article>

    <Article ID="444">
        <author>John Silas</author>
        <pubDate>01/01/2017</pubDate>  
        <Translations>
            <lang1 country="england">French</lang1>
            <lang2 country="france">Test</lang2> 
            <lang3 country="china">Game</lang3>
        </Translations>
    </Article>
</Articles>';

$file = $_SERVER['DOCUMENT_ROOT']. "/st/test.xml";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);

$xpath = new DOMXpath($doc);

** $elements = $xpath->query("//*[@country='england']");**

if (!is_null($elements)) {
  foreach ($elements as $element) {
    echo "<br/>[". $element->nodeName. "]";

    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
      echo $node->nodeValue. "\n";
    }
  }
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.