2017-02-10 106 views
1

在XML架構中,如何定義呈現如下所示的ComplexType帶有屬性和字符串內容的XML元素

<ValidationError code="X501">I love cats!</ValidationError> 

如果我嘗試以下方法我的工具*會說,這不是有效的

 <xsd:complexType name="ValidationErrorType"> 
      <xsd:attribute name="code" type="xsd:string"></xsd:attribute> 
      <xsd:simpleContent> 
       <xsd:extension base="xs:string"/> 
      </xsd:simpleContent> 
     </xsd:complexType> 

工具,我用的都是Altova公司的XMLSpy的可視化編輯和WSDL2Java生成的類

更新:我有試圖another flavour

 <xsd:complexType name="ValidationErrorType"> 
      <xsd:simpleContent> 
       <xsd:extension base="xs:string"> 
        <xsd:attribute name="code" type="xsd:string" /> 
       </xsd:extension> 
      </xsd:simpleContent> 
     </xsd:complexType> 

的Altova說Invalid XML schema: 'Value 'xs:string' is not allowed for attribute 'base'.'

回答

1

好吧,我學到的教訓

  1. 屬性可以擴展simpleContent必須出現內extension
  2. 我輸錯在我的第二個例子中,xsd前綴這幾乎是正確

正確的格式是

 <xsd:complexType name="ValidationErrorType"> 
      <xsd:simpleContent> 
       <xsd:extension base="xsd:string"> 
        <xsd:attribute name="code" type="xsd:string" /> 
       </xsd:extension> 
      </xsd:simpleContent> 
     </xsd:complexType> 
相關問題