Xml Xpath通配符只返回第一个元素

Xml Xpath通配符只返回第一个元素,xml,xpath,schematron,Xml,Xpath,Schematron,我正在编写一个schematron来验证以下xml文件: <root version="1.0"> <zone map="fields.map" display_name="Fields"> <zone.rectangles> <rectangle h="2" w="2" x="0" y="0" /> </zone.rectangles> </zone>

我正在编写一个schematron来验证以下xml文件:

<root version="1.0">
    <zone map="fields.map" display_name="Fields">
        <zone.rectangles>
            <rectangle h="2" w="2" x="0" y="0" />
        </zone.rectangles>
    </zone>
</root>
另一方面,这一条是有效的:

有效:

<root version="1.0">
    <zone map="fields.map" display_name="Fields">
        <zone.map>fields.map</zone.map>
        <zone.rectangles>
            <rectangle h="2" w="2" x="0" y="0" />
        </zone.rectangles>
    </zone>
</root>
<root version="1.0">
    <zone display_name="Fields">
        <zone.map>fields.map</zone.map>
        <zone.rectangles>
            <rectangle h="2" w="2" x="0" y="0" />
        </zone.rectangles>
    </zone>
</root>

有什么想法吗?我觉得我离得太近了

我最终通过使用一个变量使它工作起来

我使用了这个schematron:

<schema xmlns="http://purl.oclc.org/dsdl/schematron">
    <pattern>
        <title>Attribute usage</title>
        <!-- Elements that contains a dot in their name -->
        <rule context="*[contains(name(), '.')]">
            <!-- Take the part after the dot -->
            <let name="attr_name" value="substring-after(name(), '.')" />
            <!-- Check that there is no parent's attributes with the same name -->
            <assert test="count(../@*[name() = $attr_name]) = 0">
                The attribute <name /> is defined twice.
            </assert>
        </rule>
    </pattern>
</schema>

多亏了艾里克兰迪

仔细阅读。当您在
[方括号表达式]
中,并且希望引用表达式之前的上下文元素时,这非常有用。这样就不需要变量来访问方括号之外的元素。
count(XYZ)=0
实际上相当于
not(XYZ)
。我不明白的是,你的回路在哪里?您的代码没有检查每个属性,是吗?@Tomalak:第一个循环发生在上下文选择器中,在我的文档中的每个元素上都有第一个通配符
*
。然后,第二个循环在父对象的每个属性上的
。/@*
中发生。顺便说一句,谢谢你的
not()
等价于
count()=0
!啊,是的,这是有道理的。我没有正确地阅读代码。
for attribute in element.attributes:
    for child in element.children:
        if child.name == element.name + "." + attribute.name:
            raise Error
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
    <pattern>
        <title>Attribute usage</title>
        <!-- Elements that contains a dot in their name -->
        <rule context="*[contains(name(), '.')]">
            <!-- Take the part after the dot -->
            <let name="attr_name" value="substring-after(name(), '.')" />
            <!-- Check that there is no parent's attributes with the same name -->
            <assert test="count(../@*[name() = $attr_name]) = 0">
                The attribute <name /> is defined twice.
            </assert>
        </rule>
    </pattern>
</schema>
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
    <pattern>
        <title>Attribute usage</title>
        <!-- Elements that contains a dot in their name -->
        <rule context="*[contains(name(), '.')]">
            <!-- Check that there is no parent's attributes with the same name -->
            <assert test="not(../@*[name() = substring-after(name(current()), '.')])">
                The attribute <name /> is defined twice.
            </assert>
        </rule>
    </pattern>
</schema>