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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:57:50+00:00 2026-05-17T22:57:50+00:00

I’m having trouble adding Javascript as CDATA to .xml files. Is that possible? I’m

  • 0

I’m having trouble adding Javascript as CDATA to .xml files. Is that possible? I’m starting from:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 <head>
 <title>XHTML 5 Example</title>
 <script type="application/javascript">
 <![CDATA[
 function loadpdf() {
    document.getElementById("pdf-object").src="http://www.w3.org/TR/xhtml1/xhtml1.pdf";
 }
 ]]>
 </script>
 </head>
 <body onload="loadpdf()">
 <p>This is an example of an
 <abbr title="Extensible HyperText Markup Language">XHTML</abbr> 1.0 Strict document.<br/>
 <img id="validation-icon"
    src="http://www.w3.org/Icons/valid-xhtml10"
    alt="Valid XHTML 1.0 Strict"/><br/>
 <object id="pdf-object"
    type="application/pdf"
    data="http://www.w3.org/TR/xhtml1/xhtml1.pdf"
    width="100"
    height="500"/>
 </p>
 </body>
</html>

example#2 (I changed the width value to pass w3c validation)

can this file be saved as a well-formed .xml file, and should the javascript execute if so?

  • 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-17T22:57:50+00:00Added an answer on May 17, 2026 at 10:57 pm

    When putting a JavaScript script tag or CSS style tag in XHTML, it is good form to place it in a <![CDATA[ ]]> tag. However, it will be viewed by the browser as part of your script. To prevent this part of the script from executing, simply comment out your <![CDATA[]]> tag:

    <script type="text/javascript">
    /* <![CDATA[ */
    
    /* ]]> */
    </script>
    

    OR

    <style type="text/css">
    /* <![CDATA[ */
    
    /* ]]> */
    </style>
    

    This will allow you to use special characters in XML without needing to encode them, and will allow your script to execute properly without errors. Additionally, the multi-line comment is a good choice should white-space ever be parsed out.

    EDIT to add:

    I believe the object element should have a closing tag (despite being empty) similar to how you should add a closing tag to the script tag when an external source is specified. I haven’t been able to verify this as of yet.

    As for XHTML, the recommended file extension is still .html, however you may save the file with any file extension (even .pdf). Just because you have labeled it as a different file extension doesn’t mean the content is necessarily valid for that extension, and doesn’t mean that applications will be able to read the file.

    Saving XHTML as a .xml file is perfectly legitimate, and if it is properly formed XHTML it will be fully parsable by any application that can parse XML. If a web browser opens an XML file it will likely display the contents as an XML tree. If you would like the web browser to read it as a webpage, you should save it as such (.html)

    A word of caution: White space and line breaks are preserved in XML text nodes. If you parse a page using an XML parser and you’ve indented for readability purposes, the parser will include the line breaks and newline characters in the output.

    <div>
      <span>
        Some text
        on multiple
        lines
      </span>
      <span>
        Some text
        on multiple
        lines
      </span>
    </div>
    

    The contents of the span will essentially be: "\n  Some text\n  on multiple\n  lines\n"

    A way around this is to extend the ending angle bracket to the beginning of a text node:

    <div
      ><span>Some text on one line</span
      ><span>Some text on one line</span
    ></div>
    

    This works because extra white-space within an element is ignored.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.