XSD syntax for XML attributes with namespace

后端 未结 2 421
粉色の甜心
粉色の甜心 2021-01-18 16:47

I have an xml fragment for which I need to write XSD


  <         


        
相关标签:
2条回答
  • 2021-01-18 17:02

    Use the below two schemas

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/sca/1.0" xmlns:id="http://xmlns.oracle.com/id/1.0" xmlns:sca="http://xmlns.oracle.com/sca/1.0">
      <xs:import namespace="http://xmlns.oracle.com/id/1.0" schemaLocation="id.xsd"/>
      <xs:element name="root">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="sca:service"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="service">
        <xs:complexType>
          <xs:attribute name="name" use="required" type="xs:NCName"/>
          <xs:attribute ref="id:number" use="required"/>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    For ID

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/id/1.0" xmlns:id="http://xmlns.oracle.com/id/1.0" xmlns:sca="http://xmlns.oracle.com/sca/1.0">
      <xs:import namespace="http://xmlns.oracle.com/sca/1.0" schemaLocation="Untitled2.xsd"/>
      <xs:attribute name="number" type="xs:integer"/>
    </xs:schema>
    
    0 讨论(0)
  • 2021-01-18 17:04

    You actually need at least as many XSD files as namespaces since one XSD file can target only one namespace, or none.

    Since your root element is in one namespace, and the attribute in another, you need then two files at least. You "link" them through an xsd:import.

    Top XSD:

    <?xml version="1.0" encoding="utf-8"?>
    <!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
    <xsd:schema xmlns="http://xmlns.oracle.com/sca/1.0" xmlns:id="http://xmlns.oracle.com/id/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/sca/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:import schemaLocation="xsd-syntax-for-xml-attributes-with-namespace1.xsd" namespace="http://xmlns.oracle.com/id/1.0" />
      <xsd:element name="root">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="service">
              <xsd:complexType>
                <xsd:attribute name="name" type="xsd:string" use="required" />
                <xsd:attribute ref="id:number" use="required" />
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    

    xsd-syntax-for-xml-attributes-with-namespace1.xsd

    <?xml version="1.0" encoding="utf-8"?>
    <!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
    <xsd:schema xmlns="http://xmlns.oracle.com/id/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/id/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:attribute name="number" type="xsd:unsignedShort" />
    </xsd:schema>
    
    0 讨论(0)
提交回复
热议问题