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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:18:43+00:00 2026-05-29T23:18:43+00:00

I have a xml called sample.xml <?xml version=1.0?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don’t forget

  • 0

I have a xml called sample.xml

<?xml version="1.0"?>
<note>
         <to>Tove</to>
          <from>Jani</from>
          <heading>Reminder</heading>
          <body>Don't forget me this weekend!</body>
</note> 

and sample.xsd to validate sample.xml

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">

         <xs:element name="note">
                  <xs:complexType>
                           <xs:sequence>
                                    <xs:element name="to" type="xs:string"/>
                                    <xs:element name="from" type="xs:string"/>
                                    <xs:element name="heading" type="xs:string"/>
                                    <xs:element name="body" type="xs:string"/>
                           </xs:sequence>
                  </xs:complexType>
         </xs:element>

</xs:schema> 

I’m trying to validate sample.xml through my php code and since there are no namespaces in sample.xml I’m adding the namespace from the php code.

<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');

$file = 'sample.xml';
$schema = 'sample.xsd';
$doc = new DOMDocument();
$doc->load($file);

$doc->createAttributeNS('http://www.w3schools.com', 'xmlns');
$doc->createAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsi');
$doc->createAttributeNS('http://www.w3schools.com sample.xsd', 'xsi:schemaLocation');

print $doc->saveXML();


if ($doc->schemaValidate($schema)) {
         print "$file is valid.\n";
} else {
         print "$file is invalid.\n";
}
?>

and when I run the code I get the following error

Fatal error: Uncaught exception ‘DOMException’ with message ‘Namespace
Error’ in /var/www/xsd/test_sample.php:20 Stack trace: #0
/var/www/xsd/test_sample.php(20):
DOMDocument->createAttributeNS(‘http://www.w3.o…&#8217;, ‘xmlns:xsi’) #1
{main} thrown in /var/www/xsd/test_sample.php on line 20

if I comment out the line $doc->createAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsi'); the code runs fine but then I’m not able to validate the xml as it says sample.xml is invalid

Can some one help me with this code?

  • 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-29T23:18:45+00:00Added an answer on May 29, 2026 at 11:18 pm

    First thing I would fix would be to also use appendChild on your document element. Without that, the attribute is created but not appended to your tree.

    For validation, please take a look at this link.

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

Sidebar

Related Questions

I have the following defined in a file called build-dependencies.xml <?xml version=1.0 encoding=UTF-8?> <project
I have an XML file called categories.xml. Here is a sample of the file:
Say I have a table called xml that stores XML files in a single
I have the following snippet: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
I have a question about XML parsing. I was experimenting with a sample program
Below is sample code from a Module I have in my Flex app. I
Suppose I have this XML as a string: <calles> <calle> <nombre>CALLAO AV.</nombre> <altura>1500</altura> <longitud>-58.3918617027</longitud>
I have xml where some of the element values are unicode characters. Is it
I have xml files I want to read in and store in the database.
I have XML files in a directory that I wish to get over to

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.