2013-07-05 87 views
1

我試圖寫的以下XML示例XSD架構:這個XML應該是什麼模式?

<?xml version="1.0" encoding="UTF-8"?> 
<locs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="loc.xsd"> 
    <loc required="true" comment="A comment">ABC</loc> 
</locs> 

我試圖執行模式中的這些規則的規則:

  1. 根元素locs必須具有1種以上的元素的loc
  2. loc必須具有兩個屬性:requiredboolean & commentstring,非零長度的,不共mposed完全空白的空間和/或標點符號
  3. loc必須有一個字符串值,非零長度,而不是完全由空格和/或標點符號

我走了這麼遠用我loc.xsd如下:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="locs"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="loc" minOccurs="1" maxOccurs="unbounded"> 
        <xs:complexType> 
         <xs:complexContent> 
          <xs:extension base="xs:string"> 
           <xs:attribute name="required" type="xs:boolean" use="required"/> 
           <xs:attribute name="comment" type="xs:string" use="required"/> 
          </xs:extension> 
         </xs:complexContent> 
        </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

有了這個,我能夠執行規則1 & 2,但是,第三個規則未強制執行,因此類似下面的條目是有效的:

<loc required="false" comment="Another comment"/> 

我錯過了什麼?我已經花了幾個小時了!

回答

1

你非常接近。

你說的第三個約束不被強制執行,但你看不說明不執行的例子:元素

<loc required="false" comment="Another comment"/> 

具有零長度字符串的內容。您告訴XSD驗證程序,當您說loc的類型是xs:string的擴展時,那是可以的。

您需要採取的方法由JirkaŠ。的答案說明。如果您對任何非空字符串感到滿意,則可以逐字採用該解決方案。但在此之前,確保你願意接受這樣一個實例:

<loc required="false" comment="&#x9; "> </loc> 

如果你沒有,那麼你的要求是不是評論和LOC應該有字符串值,也不能說他們應該有非空字符串值,但更嚴格一些。理想情況下,您希望它們具有有用的,明智的值,但是可能無法正式定義一組有用的註釋或一組有用的loc值。有些人表示他們想要一個非空字符串,它不是完全由空格和標點符號組成(因此它至少有一個字符與類\w匹配)。

<xs:simpleType name="nonEmptyNonWSString"> 
    <xs:restriction base="xs:string"> 
    <xs:minLength value="1" /> 
    <xs:pattern value="(\W)*\w(\W|\w)*"/> 
    </xs:restriction> 
</xs:simpleType> 
+0

您提出了一個很好的空白處!我改進了我的定義,以照顧它。 –

2

嘗試這一個 - 它可以工作

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:simpleType name="nonEmptyString"> 
     <xs:restriction base="xs:string"> 
      <xs:minLength value="1" /> 
      <xs:whiteSpace value="collapse" /> 
     </xs:restriction> 
    </xs:simpleType> 

    <xs:complexType name="loc_type"> 
     <xs:simpleContent> 
      <xs:extension base="nonEmptyString"> 
       <xs:attribute name="required" type="xs:boolean" use="required"/> 
       <xs:attribute name="comment" type="nonEmptyString" use="required"/> 
      </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 

    <xs:element name="locs"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="loc" type="loc_type" minOccurs="1" maxOccurs="unbounded" /> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 
0

謝謝你們的答案,希望我可以接受他們兩人!這是我最後通過的(也修改了這個問題以消除混淆):

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <!-- Must be a string of non-zero length, not composed entirely of whitespace and punctuation marks --> 
    <xs:simpleType name="nonEmptyString"> 
     <xs:restriction base="xs:string"> 
      <xs:minLength value="1"/> 
      <xs:whiteSpace value="collapse"/> 
      <xs:pattern value="(\W)*\w(\W|\w)*"/> 
     </xs:restriction> 
    </xs:simpleType> 
    <xs:element name="locs"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="loc" minOccurs="1" maxOccurs="unbounded"> 
        <xs:complexType> 
         <xs:simpleContent> 
          <xs:extension base="nonEmptyString"> 
           <xs:attribute name="required" type="xs:boolean" use="required"/> 
           <xs:attribute name="comment" type="nonEmptyString" use="required"/> 
          </xs:extension> 
         </xs:simpleContent> 
        </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
</xs:schema>