我想检查是否存在具有特定属性的孩子。
E.g。
<?xml version="1.0" encoding="UTF-8"?>
<mc:PropertyGroupStructure xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
xmlns:mc="de.mycompany.propertygroupstructure"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="de.mycompany.propertygroupstructure file:PGroupStructure.xsd">
<mc:Property id="awesome" rename="Awesome Renaming" />
<mc:Category name="Common">
<mc:Property id="Note"/>
<mc:Property id="Other" />
</mc:Category>
<mc:Property id="aaa"></pc:Property>
</mc:PropertyGroupStructure>
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified"
vc:minVersion="1.1" targetNamespace="de.mycompany.propertygroupstructure"
xmlns="de.mycompany.propertygroupstructure">
<xs:element name="PropertyGroupStructure" type="PropertyGroupStructureType">
<xs:unique name="UniqueId">
<xs:selector xpath="./* | ./*/*"/>
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>
<xs:complexType name="PropertyGroupStructureType">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element maxOccurs="unbounded" minOccurs="0" name="Property" type="PropertyType"/>
<xs:element maxOccurs="unbounded" minOccurs="0" name="Category" type="CategoryType"/>
</xs:choice>
<xs:assert test="Property[@id eq 'awesome']" />
</xs:complexType>
<xs:complexType name="PropertyType">
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="rename" type="xs:string" />
<xs:assert test="(@id ne 'awesome') or
(@id eq 'awesome' and exists(@rename) and (@rename eq 'Awesome Renaming')) />
</xs:complexType>
<xs:complexType name="CategoryType">
<xs:sequence>
<xs:element name="Property" type="PropertyType" maxOccurs="unbounded" minOccurs="1"/>
</xs:sequence>
<xs:attribute name="name" use="required"/>
</xs:complexType>
</xs:schema>
但是Xerces总是告诉我&#34;断言没有成功&#34;。
也许XPath语法错了?
编辑:我用完整的示例替换了原始的简化示例。
答案 0 :(得分:1)
主要问题是您的断言正在针对ele/@name
进行测试,但您的XML已经ele/@attr
。纠正这个问题并简化产生以下断言,
<xs:assert test="ele[@attr eq 'awesome']" />
将根据请求成功验证您的XML。