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

  • Home
  • SEARCH
  • 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 4017456
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:51:47+00:00 2026-05-20T09:51:47+00:00

$xml = ‘<p><a>1</a><b><c>1</c></b></p>’; $dom = new DomDocument; $dom->loadXML($xml); $p = $dom->childNodes->item(0); echo $dom->saveXML($p); the

  • 0
$xml = '<p><a>1</a><b><c>1</c></b></p>';
$dom = new DomDocument;
$dom->loadXML($xml);
$p   = $dom->childNodes->item(0);
echo $dom->saveXML($p);

the above will print back

<p>
  <a>1</a>
  <b><c>1</c></b>
</p>

assume need to replace the p node/eleemnt to new_p
what is the ideal way except do a loop like below? (below is doable)

$fragment = '';
foreach ($p->childNodes as $a)
{
  $fragment .= $dom->saveXML($a);
}

$new_doc = new DomDocument;
$new_doc->loadXML('<new_node/>');
$f = $new_doc->createDocumentFragment();
$f->appendXML($fragment);
$new_doc->documentElement->appendChild($f);
echo $new_doc->saveXML();

expected results

<new_node><a>1</a><b><c>1</c></b></new_node>
  • 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-20T09:51:48+00:00Added an answer on May 20, 2026 at 9:51 am

    As Mark already pointed out, manipulating XML is easiest with XSLT. And you don’t have to write any loops, the thinking is done by the XSLT processor of your choice.

    A simple how-to with XSLT

    Here’s how the XSLT might look like (Google for “Identity transform XSLT” for some tutorials).

    The basics are simple: this type of XSLT transformation copies everything as-is, unless there’s a specific rule (template-match in XSLT) that specifies an exception (in this case for <p> elements). Note: it doesn’t matter how deep your p-tags are nested, which makes it ideal for transforming XML.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <!-- identity transform -->
        <xsl:template match="node() | @*">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
    
        <!-- rename "p" with "new_p", copy everything inside p -->
        <xsl:template match="p">
            <new_p>
                <xsl:apply-templates select="@* | node()"/>
             </new_p>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Calling XSLT from PHP

    This is relatively straightforward, since PHP has a built-in module for XSL. Here’s how you can do it (here’s more information):

    // create an XSLT processor and load the stylesheet as a DOM 
    $xproc = new XsltProcessor();
    $xslt = new DomDocument;
    $xslt->load('yourstylesheet.xslt');    // this contains the code from above
    $xproc->importStylesheet($xslt);
    
    
    // your DOM or the source XML (copied from your question)
    $xml = '<p><a>1</a><b><c>1</c></b></p>';
    $dom = new DomDocument;
    $dom->loadXML($xml);
    
    // do the transformation
    if ($xml_output = $xproc->transformToXML($dom)) {
        echo $xml_output;
    } else {
        trigger_error('Oops, XSLT transformation failed!', E_USER_ERROR);
    } 
    

    Output is as expected (optional indentation can be set with <xsl:output indent="yes"/>:

    <new_p>
        <a>1</a>
        <b><c>1</c></b>
    </new_p>
    

    As you can see: no loops or iterations 😉

    PS: XSLT is a widely adopted and stable standard. You don’t have to worry about proper escaping, parsing issues with CDATA sections or entities, because XSLT guarantees the output to be valid XML. This saves a whole lot of headaches as opposed to doing this by hand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

?xml version=1.0 encoding=UTF-8 ?> <IPAD version=1.0> <articleSnapshot> <item> <categoryName>Kerala</categoryName> <oid>2915</oid> <title>ÎÆcÈÏ¢: ÄßøáJW ÕçKAá¢</title> <thumbImage>http://122.248.234.55/v3/repository/images/news/bar-liquor_thuthumb_2915.jpg</thumbImage>
The XML Schema Part 2 specifies that an instance of a datatype that is
This XML file contained archived news stories for all of last year. I was
The XML I'm trying to validate is as follows: <root> <element attribute=foo> <bar/> </element>
Which XML validation tools can you recommend for both performance and accuracy, each of
An XML attribute declared as xs:boolean can acceptable be true, false, 0 or 1.
I have a complete XML document in a string and would like a Document
I have an XML object (loaded using XMLHTTPRequest 's responseXML ). I have modified
I'm generating some XML documents and when it comes to the address part I
Given the following XML: <current> <login_name>jd</login_name> </current> <people> <person> <first>John</first> <last>Doe</last> <login_name>jd</login_name> </preson> <person>

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.