B
B
banny_name2015-08-31 13:50:54
PHP
banny_name, 2015-08-31 13:50:54

How to find out the path to the xml element?

there is xml:

<w:body>
    <w:p>
        <w:text>Текст</w:text>
    </w:p>
</w:body>

how can I find out all the way to "Text" ...
I need to operate with this data ..
I did not find the right one in xPath

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kontiv, 2017-06-15
@kontiv

for example, on Xquery you can do this

declare namespace w = "w";

declare function local:recursion ( $node as element() ) as xs:string {
  if ( $node/parent::* )
  then (
    local:recursion ( $node/parent::* ) || "/" || $node/name() ||
    "[" || count( $node/preceding-sibling::*[ name() = $node/name() ] )+1 || "]"
   )
  else ( $node/name() )
};
 
let $xml := 
   <w:body>
      <w:p>
          <w:text>Текст</w:text>
      </w:p>
      <w:p>
          <w:text>Текст</w:text>
          <w:text>Текст</w:text>
      </w:p>
  </w:body>
  
for $node in $xml//w:text
return
  local:recursion( $node )

The answer will be something like this:
w:body/w:p[1]/w:text[1]
w:body/w:p[2]/w:text[1]
w:body/w:p[2]/ w:text[2]

R
Rowdy Ro, 2015-09-02
@rowdyro

make an xpath request, and then from the found element go up to the parents.
php.net/manual/ru/class.domnode.php - parentNode

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question