2010-12-21 47 views
13

我試圖創建一個能夠捕獲XML,看起來像這樣的XML架構:SimpleType和Attribute的XML模式?

<tagname description="simple string type attribute"> 
false <!-- simple boolean type --> 
</tagname> 

,但我遇上了困難。是否有可能定義一個模式來捕捉這個或我是否在snipe hunt

回答

33

在這裏你去

<xs:element name="tagname"> 
     <xs:complexType> 
      <xs:simpleContent> 
       <xs:extension base="xs:boolean"> 
        <xs:attribute name="description" type="xs:string" use="required"/> 
       </xs:extension> 
      </xs:simpleContent> 
     </xs:complexType> 
    </xs:element> 

這裏是驗證樣品

<tagname xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="file:/C:/Untitled2.xsd" description="123"> 
    true 
</tagname> 
1

謝謝你,謝謝你,謝謝你。我一直在努力解決這個問題一段時間,即使實際的XML示例非常簡單,如何定義模式也不是那麼明顯。我最大的問題是如何構建一個JAXB類來處理這個問題。直到我看到你的模式定義並通過xjc運行它,我才能夠看到如何在JAXB中進行設置。 JAXB java類是非常直觀的恕我直言,我從來沒有猜到如何設置它。我嘗試了幾種不同的方式讓這個工作沒有任何成功。

下面是從您的發佈架構生成的JAXB java類的示例。關鍵是用場上的@XmlValue註釋(你也可以用它在球場上的吸氣劑,但除去XmlAccessorType註釋:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { "value" }) 
@XmlRootElement(name = "tagname") 
public class Tagname { 

    @XmlValue 
    protected boolean value; 
    @XmlAttribute(name = "description", required = true) 
    protected String description; 

    public boolean isValue() { 
    return value; 
    } 

    public void setValue(boolean value) { 
    this.value = value; 
    } 

get and set for description omitted. 

這裏是從給定的類編組JAXB XML文檔:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<tagname description="The Description">true</tagname> 

我希望增加這一幫助別人誰是同樣的模糊不清的問題掙扎。