Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:30:01+00:00 2026-05-23T19:30:01+00:00

The XML shown is a simplified version of what I’m working with. I’m using

  • 0

The XML shown is a simplified version of what I’m working with. I’m using PHP, and DOMDocument and DOMXPath.

I have a number of similar nodes that are adjacent to each other, but have slightly different children. Given that I can locate one of these nodes, based on the content of the children, how can I use XPath to also grab the preceding node, the originally selected node, the following node, and the following node two positions later.

Here’s the sample XML:

<w:p>        
    <w:r>
        <w:rPr>...</w:rPr>
        <w:t>Text</w:t>
    </w:r>
    <w:r>
        <w:rPr>...</w:rPr>
        <w:fldChar w:fldCharType="begin" />
    </w:r>
    <w:r>
        <w:rPr>...</w:rPr>
        <w:instrText> MERGEFIELD  [PatName]  \* MERGEFORMAT  </w:instrText>
    </w:r>
    <w:r>
        <w:rPr>...</w:rPr>
        <w:fldChar w:fldCharType="separate" />
    </w:r>
    <w:r>
        <w:rPr>...</w:rPr>
        <w:t>[PatName]</w:t>
    </w:r>
    <w:r>
        <w:rPr>...</w:rPr>
        <w:fldChar w:fldCharType="end" />
    </w:r>
</w:p>

The starting node to work with is the w:instrText node, with XPath looking like:

//w:r[contains(w:instrText,'MERGFIELD [PatFirstName]')].

Then I can use the preceding-sibling axis to locate the previous item. The XPath looks like:

//w:r[contains(w:instrText,'MERGFIELD [PatFirstName]')]/preceding-sibling::w:r[1].

Then I’d like to grab the original w:r containing w:instrText, and the two remaining w:r nodes containing w:fldChar, leaving the w:t node out of the selection. But my attempts to write XPath for this become unravelled:

//w:r[contains(w:instrText,'MERGEFIELD  [PatFirstName]')]/preceding-sibling::w:r[1]/following-sibling::w:r[1 and 2] 

grabs too many nodes, probably because the original contains condition does not apply to the Following-sibling conditions).

Ultimately, the following entries would be extracted from that snippet.

    <w:r>
        <w:rPr>...</w:rPr>
        <w:fldChar w:fldCharType="begin" />
    </w:r>
    <w:r>
        <w:rPr>...</w:rPr>
        <w:instrText> MERGEFIELD  [PatName]  \* MERGEFORMAT  </w:instrText>
    </w:r>
    <w:r>
        <w:rPr>...</w:rPr>
        <w:fldChar w:fldCharType="separate" />
    </w:r>
    <w:r>
        <w:rPr>...</w:rPr>
        <w:fldChar w:fldCharType="end" />
    </w:r>

It’s important that relative nodes are used for the search, since there may be other similar looking node combinations in the XML.

Some of you may recognise this XML as the Word 2003 XML format for a mergefield, with much of the cruft removed. I’m trying to isolate the w:r node containing the w:t, so I can update that, and delete the surrounding nodes used to identify it as a mergefield.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:30:02+00:00Added an answer on May 23, 2026 at 7:30 pm

    I’ve come to the conclusion that what I’m asking is too ambitious for XPath alone. follow-sibling and preceding-sibling axes are 1 or all deals (unless someone can show me otherwise).

    I’ve ended up using XPath to get the w:t node I’m interested in replacing, based on the MERGEFIELD, and then I walk the DOM, using DOMDocument in PHP to remove the other nodes.

    Here’s the XPATH I ended up using, expressed as an assignment to a variable in PHP.

    $query = '//w:r[preceding-sibling::w:r[2][contains(w:instrText,\'MERGEFIELD  '.$mergeField.'\')]]/w:t';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using the following code to output all the nodes from the xml shown.
best shown by a simplified example: <?xml version=1.0 encoding=utf-8?> <root> <A name=wrong></A> <B>not this</B>
I have an xml as shown: <?xml version=1.0 encoding=utf-8?> <Query_advanced> <Query>hy</Query> <Attribute Name1=Patient's Age
I’m using Linq to extract values from some XML. Shown below is a simplified
i have an xml file sitemap.xml as shown below ..i need to add one
I have a bit of xml file named Sample.xml which is shown below <?xml
I using a dataset to read an xml file as shown below DataSet ds
I have a httpservice that returns xml data. <mx:HTTPService id=httpService url=data/Software.xml resultFormat=e4x result=httpResult_handler(event) fault=Alert.show('XML
does Delphi 2010 have C# Style XML comments that show up when hovering over
I have been trying to output XML with PHP but encountered a strange(!) error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.