0
<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 ?

Edited by Osagie_1: non

2
Contributors
1
Reply
18
Views
2 Years
Discussion Span
Last Post by mangel.murti
0

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";
    }
  }
}
?>

Edited by mangel.murti: improve code

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.