Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and use XML namespace?

I want a page like this:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:m="mine.xsd">
    <m:dialog m:title="Hello">Hi there!</m:dialog>
</html>

How can I write "mine.xsd"?

like image 456
JSPDeveloper01 Avatar asked Jul 18 '10 14:07

JSPDeveloper01


People also ask

How do you create a namespace in XML?

XML Namespaces - The xmlns Attribute When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

What is XML namespace explain with example?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.

Why is namespace used in XML?

One of the primary motivations for defining an XML namespace is to avoid naming conflicts when using and re-using multiple vocabularies. XML Schema is used to create a vocabulary for an XML instance, and uses namespaces heavily.


2 Answers

xsd files are XML Schema files, read about it. Some more here.

A simple example:

XMLSchema1.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Types"
    targetNamespace="http://tempuri.org/"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/"
    xmlns:mstns="http://tempuri.org/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:simpleType name="Types">
    <xs:annotation>
      <xs:documentation>.NET types</xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:enumeration value="String" />
      <xs:enumeration value="Int16" />
      <xs:enumeration value="Int32" />
      <xs:enumeration value="Int64" />
      <xs:enumeration value="DateTime" />
      <xs:enumeration value="Double" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="DataSize">
    <xs:annotation>
      <xs:documentation>Number of bytes of the data</xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:int" />
  </xs:simpleType>

  <!-- ... -->

</xs:schema>

Then in your XML file you can use:

<?xml version="1.0" encoding="utf-8" ?>

<ValueSet
  xmlns="http://tempuri.org/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://tempuri.org/ XMLSchema1.xsd">

  <Values>
    <Value Name="Stats" Type="Int32" DataSize="4" />
    <Value Name="Time" Type="DateTime" DataSize="4" />
    <Value Name="Some" Type="Double" DataSize="4" />
    <Value Name="Other" Type="Double" DataSize="4" />
  </Values>

</ValueSet>
like image 186
BrunoLM Avatar answered Oct 22 '22 04:10

BrunoLM


You can write that XSD file yourself, by hand - you just need to study what makes up the XML schema, and get to know how to write that code yourself. Google or Bing for "XML Schema Tutorial" should give you a ton of hits (e.g. the W3Schools XML Schema Tutorial).

Or you could use Visual Studio to do this:

Example image

  • open the XML file you want to handle in Visual Studio
  • From the XML menu, choose the Create Schema menu item

This will generate an XML schema from your XML file.

Note: this is a good starting point - but it's by no means perfect. Especially with smaller XML files, there are lots of things the generation process cannot know and it just has to make certain assumptions - which might be right or might be wrong. You will need to have a look at the XML schema file for sure - and that's where the know-how from the first option comes into play very handily!

like image 43
marc_s Avatar answered Oct 22 '22 04:10

marc_s