以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  同样的元素名称,属性值不同,怎么写schema呢?谢谢  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=43231)


--  作者:wise
--  发布时间:2/12/2007 1:41:00 AM

--  同样的元素名称,属性值不同,怎么写schema呢?谢谢
比如要写这样一个例子:
有三种Node元素,有一个type属性,可以为a或b或c,现在要求3种里面随意出现一个
我如下写,好像不行,不知道为什么
<xsd:choice>
  <xsd:element name="Node" Type="AType">
    <xsd:complexType>
      <xsd:attribute name="type" Type="xsd:string" fixed="a"/>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="Node" Type="BType">
    <xsd:complexType>
      <xsd:attribute name="type" Type="xsd:string" fixed="b"/>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="Node" Type="CType">
    <xsd:complexType>
      <xsd:attribute name="type" Type="xsd:string" fixed="c"/>
    </xsd:complexType>
  </xsd:element>
</xsd:choice>

这样写好像不行,可能原因是我用了同样的Name,都是Node,但是我就是要同样的Name阿,我不知道怎么写了:(
大虾帮帮忙吧


--  作者:upzone
--  发布时间:2/12/2007 5:08:00 PM

--  
Attribute没有 choice的属性,Attribute必须要依赖于Element存在,我换了一种方法变通实现.
1.Schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:element name="ChoiceAttribute">
  <xs:annotation>
   <xs:documentation>Att</xs:documentation>
  </xs:annotation>
  <xs:complexType>
   <xs:sequence maxOccurs="unbounded">
    <xs:element name="Nodes" type="NodeType"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:complexType name="NodeType" abstract="true">
  <xs:sequence>
   <xs:element name="Node" type="xs:string"/>
  </xs:sequence>
  <xs:attributeGroup ref="ABCGroup"/>
  <xs:attribute name="C"/>
 </xs:complexType>
 <xs:attributeGroup name="ABCGroup">
  <xs:attribute name="A" type="xs:string" use="optional"/>
  <xs:attribute name="B" type="xs:string" use="optional"/>
 </xs:attributeGroup>
 <xs:complexType name="NodeTypeA">
  <xs:complexContent>
   <xs:restriction base="NodeType">
    <xs:sequence>
     <xs:element name="Node" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="A" type="xs:string" use="optional"/>
    <xs:attribute name="B" type="xs:string" use="prohibited"/>
    <xs:attribute name="C" use="prohibited"/>
   </xs:restriction>
  </xs:complexContent>
 </xs:complexType>
 <xs:complexType name="NodeTypeB">
  <xs:complexContent>
   <xs:restriction base="NodeType">
    <xs:sequence>
     <xs:element name="Node" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="B" type="xs:string" use="optional"/>
    <xs:attribute name="A" type="xs:string" use="prohibited"/>
    <xs:attribute name="C" use="prohibited"/>
   </xs:restriction>
  </xs:complexContent>
 </xs:complexType>
 <xs:complexType name="NodeTypeC">
  <xs:complexContent>
   <xs:restriction base="NodeType">
    <xs:sequence>
     <xs:element name="Node" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="A" type="xs:string" use="prohibited"/>
    <xs:attribute name="B" type="xs:string" use="prohibited"/>
    <xs:attribute name="C" type="xs:string" use="required"/>
   </xs:restriction>
  </xs:complexContent>
 </xs:complexType>
</xs:schema>

2.Sample XML

<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2007 sp1 (http://www.altova.com)-->
<ChoiceAttribute xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ChoiceAttribute.xsd">
 <Nodes xsi:type="NodeTypeA" A="String">
  <Node>String</Node>
 </Nodes>
 <Nodes xsi:type="NodeTypeB" B="String">
  <Node>String</Node>
 </Nodes>
 <Nodes xsi:type="NodeTypeC" C="String">
  <Node>String</Node>
 </Nodes>
</ChoiceAttribute>


通过继承方式来实现,不过需要在xml instance里添加属性xsi:type 来标记具体的类型
只是一种实现方式,比较麻烦。不对之处,请指教。


--  作者:wise
--  发布时间:2/13/2007 12:07:00 AM

--  
可能是我表达不清楚……我希望的xml文件是这样的
<Nodes>
  <Node type="a">我是A点</Node>
  <Node type="c">我是C点</Node>
  <Node type="b">我是B点</Node>
  <Node type="c">我又是C点</Node>
  <Node type="a">我们出现没什么规律</Node>
  <Node type="c">我是C点,想出现几遍就出现几遍</Node>
  <Node type="b">我是B点,其实我不出现也无所谓</Node>
</Nodes>
--  作者:upzone
--  发布时间:2/13/2007 12:58:00 AM

--  
Sorry,看错了,不过你这样还不如直接Node 节点循环,而Node的type attribute设定enumeration a,b,c

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:element name="Nodes">
  <xs:complexType>
   <xs:sequence maxOccurs="unbounded">
    <xs:element name="Node">
     <xs:complexType>
      <xs:attribute name="type">
       <xs:simpleType>
        <xs:restriction base="xs:string">
         <xs:enumeration value="a"/>
         <xs:enumeration value="b"/>
         <xs:enumeration value="c"/>
        </xs:restriction>
       </xs:simpleType>
      </xs:attribute>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>


--  作者:wise
--  发布时间:2/13/2007 4:00:00 PM

--  
这个办法我想过,但是有个问题
我需要当type=a的时候内容形式和b还有c都不一样
比如a节点是string
b是int
c是嵌套其他元素
我不知道怎么区分开这些node,并且与自身的type属性绑定
--  作者:wise
--  发布时间:2/14/2007 1:50:00 PM

--  
大大快显身阿,天天在线等呢,谢谢!
--  作者:gemingke
--  发布时间:3/19/2007 10:07:00 PM

--  
xml里面,同名元素不能使用不同的数据类型,除非你使用namespce,但那样会使问题更加复杂
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms