views:

40

answers:

2

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"? Thanks!

A: 

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>
BrunoLM
A: 

You can of course 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 Learn XML schema series).

Or you could use Visual Studio to do this:

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

alt text

This will generate a 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!

marc_s