10

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

2 Answers 2

8

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>
Sign up to request clarification or add additional context in comments.

2 Comments

What I want is to embed some custom tags or attributes into XHTML code without causing syntax errors. Is there any way I can "inject" my tags into <html> element using XML schemas?
@JSPDeveloper01: I think the augmentation could be done like this with xsd:annotation/xsd:appinfo in your custom xsd: stackoverflow.com/a/36289758/1915920
4

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.