开发者

Way of skipping choice tags in xml validation

I have the following XSD:

  <xs:element name="Parameter" type="complex">
  </xs:element>
  <xs:complexType name="complex">   
    <xs:choice>
      <xs:element name="MyData" type="myData"/>
      <xs:element name="String" type="xs:string"/>
    </xs:choice>
    <xs:attribute name="Name" use="required" type="xs:string"/>   
  </xs:complexType>
  <xs:complexType name="myData">     
     <xs:attribute name="X" use="required" type="xs:integer"/> 
     <xs:attribute name="Y" use="required" type="xs:integer"/> 
  </xs:complexType>

when i validate this

<Parameter Name="P"><MyData X="1" Y="2"></MyData><开发者_StackOverflow/Parameter>

everything is OK, but when i try to validate this:

        <Parameter Name="P">5r5r5r5r</Parameter>

it says "The element 'Parameter' cannot contain text. List of possible elements expected: 'MyData, String'." Is there a way to skip putting <Sring></String> around "5r5r5r5r"?


What do you really want? If you want to write inside a Parameter tag you should define it like that:

<xs:element name="Parameter" type="xs:string"></xs:element>


So it seems that you want to have a <Parameter> element that can contain other elements or text. This can be achieved by allowing mixed content by setting attribute mixed="true" on <xs:complexType> or <xs:complexContent> element.

Sample code below.

<xs:element name="Parameter">
  <xs:complexType mixed="true">
    <xs:sequence>
      <xs:element name="MyData" type="MyData"/>
    </xs:sequence>
    <xs:attribute name="Name" use="required" type="xs:string"/>
  </xs:complexType>
<xs:element name="Parameter">

Note: mixed type allows element to have any of these possible content choices

  • only text
  • only elements
  • both text and elements so that text can appear
    • before the elements
    • after the elements
    • in between the elements
    • or any combination of the above-mentioned.

You have three questions that all revolve around the same subject. If these answers don't really address your problem then I'm asking you to specify what all requirements you actually want to fulfil.

  • Is it allowed that element can contain both text and sub-elements at the same time?
  • Does the text content need to be restricted to some format?
  • Can the possible sub-elements be any elements or are they restricted to some known elements?
  • Do you need to be able to validate the sub elements, if they can be any elements?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜