简体   繁体   English

XSLT:验证下级节点是否具有相同的IDREF属性

[英]XSLT: Verify if sibbling nodes have the same IDREF attribute

Good afternoon. 下午好。

I'm fairly new to XML, XSD and XSLT and i'm facing the following issue: I have the following substructure in XSD, that can occur multiple times, which is part of a far larger structure which is irrelevant and i won't include for the sake of clarity: 我对XML,XSD和XSLT相当陌生,并且面临以下问题:我在XSD中具有以下子结构,该子结构可以多次出现,这是一个更大的结构的一部分,这是不相关的,我不会为了清楚起见,包括:

<xs:complexType name= "listavotosmocao">
<xs:choice maxOccurs="unbounded">
    <xs:element name="favor" type="pessoaref"/>
    <xs:element name="contra" type="pessoaref"/>
    <xs:element name="abstiveram" type="pessoaref"/>
</xs:choice>
</xs:complexType> 

<xs:complexType name="pessoaref">
<xs:attribute ref="id" type="xs:string"/>
</xs:complexType>

Using XLST i'm supose to asure that if an element has a certain IDREF attribute value any repetition of the same value on the other elements should trigger a message on the stdout. 我使用XLST来确保如果元素具有某个IDREF属性值,则在其他元素上重复相同值的任何操作都应在stdout上触发一条消息。 To achieve this end i wrote the following lines in XSLT: 为此,我在XSLT中编写了以下几行:

<xsl:for-each select="votacao//favor">
   <xsl:if test="(count(preceding-sibling::contra[attribute::ref= ./@ref])
   + count(following-sibling::contra[attribute::ref=./@ref]) )> 0">
        Error
   </xsl:if>
</xsl:for-each>

Which always prints Error in the html file, instead of doing so only when there are elements with the same values on IDREF. 总是在html文件中显示Error,而不是仅在IDREF上的元素具有相同值时才显示。 Both my understanding of the language and my search have not been able to help me understand this issue and how to solve it. 我对语言的理解和搜索都无法帮助我理解此问题以及如何解决。 Any ideas to why this might occur? 有什么想法可能会发生这种情况吗?

Kind regards 亲切的问候

attribute::ref= ./@ref

is the same as @ref=@ref so is true whatever value of ref, 与@ ref = @ ref相同,所以无论ref的值是什么,

I'm not sure about the relevance of the XSD fragment (which doesn't mention ref) but perhaps you want 我不确定XSD片段的相关性(没有提及ref),但也许您想要

<xsl:if test="../contra[@ref=current()/@ref][2]">
    Error
</xsl:if>

That tests if there are 2 (or more contra elements with the same ref as the current favor element, perhaps you want [1] or to use ../* rather than ../contra, it all depends. The question is not clear enough to be certain. 那测试是否有2个(或更多个具有与当前忙元素相同的ref的对位元素,也许您想要[1]或使用../*而不是../contra,这全都取决于。问题尚不清楚)足以确定。

1) try &lt; 1)尝试&lt; instead of > 代替>

2) in for-each you are looping through favor elements, in if you are counting contra elements, hope that's what you really want 2)在for-each您正在遍历favor元素, if要计算contra元素,希望那是您真正想要的

3) why count preceding and following sibling, couldn't that be in all at once like 3)为什么计数同胞之前和之后,不能一次像

<xsl:if test="count(../contra[@ref = ./@ref]) &lt; 0">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM