How to define a simple element and attribute in XSD

别来无恙 提交于 2021-01-27 13:23:42

问题


My XML looks like this:

<test attri="value">content</test>

I have trouble building this in XML Schema, since I can't give a complexType content and can't give a simpleType an attribute. I feel like there should be a very simple solution to this that I am overlooking.


回答1:


use the following:

 <xs:element name="test ">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="xs:string">
                    <xs:attribute name="attri" type="xs:string"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>



回答2:


XSD 1.0

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
  <xs:element name="test">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="attri" type="xs:string"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>


来源:https://stackoverflow.com/questions/31271512/how-to-define-a-simple-element-and-attribute-in-xsd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!