簡體   English   中英

將正則表達式模式用於 XML / XSD 文件

[英]Using Regex Pattern for Money with XML / XSD files

第一次創建模式 - 不確定這里的問題是什么。

我想使用一個正則表達式模式,它接受小數點左邊的無限位數,右邊只有兩位數(標准美元符號)作為“價格”的自定義數據類型,因為它在 XML 文檔中使用,但是我收到以下一對錯誤:

“cvc-pattern-valid:值‘219.99’對於‘money’類型的模式‘^[0-9]+.[0-9]{2}$’不是有效的。”

“cvc-attribute.3:元素‘product’上屬性‘price’的值‘219.99’就其類型‘money’而言無效

這是我的 XML 文件中“價格”返回錯誤的相關部分:

<product sku="29043" category="audio" isFeatured="no" price="219.99">
    <name>Sennheiser PC350 Gaming Stereo Headset</name>
    <description>Gaming stereo headset, 10-26000Hz, 150ohm, 10-ft cord, noise-cancelling microphone.</description>
    <colors>
      <color>Black</color>
      <color>Red</color>
    </colors>
  </product> 

這是我的 XSD 文件,其中包含相關部分:

<xs:attribute name="sku" type="xs:positiveInteger"/>
                        <xs:attribute name="category" type="section"/>
                        <xs:attribute name="isFeatured" type="yesno"/>
                        <xs:attribute name="price" type="money"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>


    <xs:simpleType name="section">
        <xs:restriction base="xs:token">
            <xs:enumeration value="audio"/>
            <xs:enumeration value="appliance"/>
            <xs:enumeration value="bathroom"/> 
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="yesno">
        <xs:restriction base="xs:token">
            <xs:enumeration value="yes"/>
            <xs:enumeration value="no"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="money">
        <xs:restriction base="xs:string">
            <xs:pattern value='^[0-9]+.[0-9]{2}$'/>
        </xs:restriction>
    </xs:simpleType>

任何指針? 如果需要,可以提供更多信息。

XSD 正則表達式模式需要完整的字符串匹配,並且^$沒有被解析為錨點。

您可以使用

<xs:simpleType name="money">
    <xs:restriction base="xs:string">
        <xs:pattern value='[0-9]+\.[0-9]{2}'/>
    </xs:restriction>
</xs:simpleType>

這匹配以一個或多個數字 ( [0-9]+ ) 開頭,然后有一個點 ( \. ,請注意它應該被轉義) 並以兩位數字 ( [0-9]{2} ) 結尾的字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM