How to generate an XML schema, where I have some nodes with namespace and others without

2

I'm trying to generate an XML schema, for future validations, where some attributes contain a specific namespace, and others do not:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:g="http://base.google.com/ns/1.0" version="2.0">
   <title>Titulo do XML</title>
   <description> Descricao do XML </description>
   <link>http://www.linkDoXml.com.br</link>
   <item>
      <g:id>11111111111</g:id>
      <title> Titulo do item </title>
      <description> Descricao do item </description>
      <g:category> Categoria 2 </g:category>
      <g:product_type>tipo 1</g:product_type>
   </item>
   <item>
      <g:id>2222222222</g:id>
      <title> Titulo do item 2</title>
      <description> Descricao do item 2</description>
      <g:category> Categoria 2 </g:category>
      <g:product_type>tipo 1</g:product_type>
   </item>
</root>

In this example, I have the "id", "category" and "product_type" attributes with the namespace "g", which is the Google Shopping namespace ( link ), and the rest of the attributes without namespace.

What would be the schema of this xml?

    
asked by anonymous 22.09.2014 / 21:47

1 answer

2

Your schema will be divided into two documents: one storing the default namespace ("") information, and the other in the google namespace ( http://base.google.com/ns/1.0 ). To generate the schema (in two files), you can use the xsd.exe utility (comes installed with the Windows SDK, or in Visual Studio). If you store the above XML content in a file named root.xml , and execute the command below:

xsd.exe root.xml

Then the following files will be generated:

root.xsd :

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:app1="http://base.google.com/ns/1.0">
  <xs:import namespace="http://base.google.com/ns/1.0" schemaLocation="root_app1.xsd" />
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="title" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
        <xs:element name="description" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
        <xs:element name="link" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
        <xs:element name="item" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element ref="app1:id" minOccurs="0" />
              <xs:element name="title" type="xs:string" minOccurs="0" />
              <xs:element name="description" type="xs:string" minOccurs="0" />
              <xs:element ref="app1:category" minOccurs="0" />
              <xs:element ref="app1:product_type" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="version" type="xs:string" />
    </xs:complexType>
  </xs:element>
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="root" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

root.app1.xsd :

<?xml version="1.0" standalone="yes"?>
<xs:schema targetNamespace="http://base.google.com/ns/1.0" xmlns:mstns="http://base.google.com/ns/1.0" xmlns="http://base.google.com/ns/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:app1="http://base.google.com/ns/1.0">
  <xs:element name="id" msdata:Prefix="g" type="xs:string" />
  <xs:element name="category" msdata:Prefix="g" type="xs:string" />
  <xs:element name="product_type" msdata:Prefix="g" type="xs:string" />
</xs:schema>
    
25.09.2014 / 22:22