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 8187565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:36:06+00:00 2026-06-07T02:36:06+00:00

I have an xml document which I transform, view in the browser and save

  • 0

I have an xml document which I transform, view in the browser and save to a directory.

I would like to add a linked stylesheet to the saved version of the file. I’ve tried str_replace as below (might be incorrect usage) and also using xsl processing instruction.

xsl processing instruction worked to a degree, if you view source in the browser, you would see the stylesheet link, however it would not save this information to the saved file!!

All I want to do is take the raw xml file, transform it with the stylesheet, save it to a directory and append the xsl stylesheet to the header of the new file, so when the newly saved xml file is opened in the browser, the stylesheet is applied automatically. Hope this makes sense!!

My code is below.

//write to the file
$id = $_POST['id'];//xml id
$path = 'xml/';//send to xml directory
$filename = $path. $id . ".xml";
$header = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '<?xml version="1.0"     encoding="UTF-8" ?><?xml-stylesheet type="text/xsl" href="../foo.xsl"?>');
$article->asXML($filename);//saving the original xml as new file with posted id

$xml = new DOMDocument;
$xml->load($filename);

$xsl = new DOMDocument;
$xsl->load('insert.xsl');

$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);

echo $proc->transformToXML($xml);

Thanks in advance!

  • 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-06-07T02:36:08+00:00Added an answer on June 7, 2026 at 2:36 am

    You could use the <xsl:processing-instruction/> tag in the XSLT-file to add the stylesheet link.

    <xsl:processing-instruction name="xml-stylesheet">
        type="text/xsl" href="../foo.xsl"
    </xsl:processing-instruction>
    

    This would produce:

    <?xml-stylesheet type="text/xsl" href="../foo.xsl"?>
    

    Alternativly, you could do it with a DOMDocument.

    $newXml = $proc->transformToXML($xml);
    
    // Re-create the DOMDocument with the new XML
    $xml = new DOMDocument;
    $xml->loadXML($newXml);
    
    // Find insertion-point
    $insertBefore = $xml->firstChild;
    foreach($xml->childNodes as $node)
    {
      if ($node->nodeType == XML_ELEMENT_NODE)
      {
        $inertBefore = $node;
        break;
      }
    }
    
    // Create and insert the processing instruction
    $pi = $xml->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="../foo.xsl"');
    $xml->insertBefore($pi, $insertBefore);
    
    echo $xml->saveXML();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document which contains nodes like following:- <a class=custom>test</a> <a class=xyz></a>
I have an XML document which looks something like this: <meadinkent> <record> <comp_div>MENSWEAR</comp_div> <sty_ret_type>ACCESSORIES</sty_ret_type>
I have got an XML document which looks something like this. <Root> <Info>....</Info> <Info>....</Info>
I have created a xslt document which formats an xml document, but I would
I have XML document which is something like <X><Y><Values><double>1.0</double><double>2.0</double></Values>... I am trying to get
I have an XML document which reads like this: <xml> <web:Web> <web:Total>4000</web:Total> <web:Offset>0</web:Offset> </web:Web>
In my Python app, I have an XML document that I'd like to transform
All, I have an XML file which I transform it using an XSLT document
I have an xml document which consists of a number of the following: -
Within Symphony CMS, I want to be able to have an XML document which

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.