使用 xsd 模式,我需要檢查元素的值字串是否包含依賴于子元素的特定子字串<random>
。<random>
當最后決議字串而不是字串“{rand}”時,應該顯示元素中定義的范圍內的亂數。這要求實際上包含字串“{rand}”。因此,例如以下兩個代碼示例應始終包含子字串“{rand}”,因為它包含一個隨機元素:
<text>Your dice rolled a value of <annotation value='counter'>{rand}<random start='1' end='6'/></annotation>.</text>
<text>Your rolled {rand} times with a dice that had <annotation value='rolls'>6<random start='1' end='4'/></annotation> eyes.</text>
為了確保這一點,我使用了以下包含斷言的 XSD 代碼:
<xs:element name="text">
<xs:complexType mixed="true">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="annotation" type="defined_annotation" minOccurs="0" maxOccurs="3"/>
</xs:choice>
</xs:sequence>
<xs:assert test="string-length() ge 3"/>
<xs:assert test="if (annotation/random) then (contains($value, '{rand}')) else not(annotation/random)"/>
</xs:complexType>
</xs:element>
<xs:complexType name="defined_annotation" mixed="true">
<xs:sequence>
<xs:element name="random" type="random" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="value" type="xs:string"/>
</xs:complexType>
可悲的是,無論我在何處或多久包含該元素,是否包含子字串“{rand}”的測驗總是失敗。如果“{rand}”是元素<text>
中包含的唯一文本,它甚至會失敗。因此,我已經使用了幾種不同的斷言變體,包括以下示例:
<xs:assert test="if (annotation/random) then (contains($value, '{rand}')) else not(annotation/random)"/>
<xs:assert test="if (random) then (contains(text/$value, '{rand}')) else not(random)"/>
<xs:assert test="if (random) then $value/contains('{rand}') else not(random)"/>
所有這些都編譯。這些都不起作用。我究竟做錯了什么?
uj5u.com熱心網友回復:
我認為您所有嘗試的主要問題$value
是僅針對斷言中的簡單型別定義,否則變數是空序列(https://www.w3.org/TR/xmlschema11-1/#assertion-validation:“ E的·管理型別定義·沒有{content type}。{variety} = simple)值是空序列")。
你可以做的只是例如
<xs:assert test="if (annotation/random) then (contains(., '{rand}')) else not(annotation/random)"/>
.
是背景關系項,即在您的情況下是text
元素節點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/495835.html