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 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 in php, I have tried : echo '<?xml version=1.0
xml has been the backbone of the service oriented application(SOA) and it will be
XML <Data> <Language Name=ru-Ru> <Item Id=1>value</Item> <Item Id=2>value2</Item> </Language> </Data> Is it possible to
?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>
XML file result from food:food-item($food:fps-photo-atlas) <portion> <food-description unit=g>Spaghetti </food-description> <food-number file-type=jpg>3 </food-number> <portion-size> <size
XML file can be seen below: # How can I code it so the
XML Schema specifies occurrence indicators (maxOccurrence, minOccurrence). Is there a best practice in which
XML: <A><B></B></A> or <A></A> I want to get all A nodes that do not
xml.etree.ElementTree.parse is choking on my xhtml file. I saw somewhere that lxml can handle
XML sample ( original link ): <records> <record index=1> <property name=Username>Sven</property> <property name=Domain>infinity2</property> <property

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.