如何针对xslt中的特定值检查所有listnode字段?

https://xsltfiddle.liberty-development.net/94AbWB6/2

在此示例中,我尝试匹配<persons>的列表,其中<person>.<lastname>与列表中的所有其他人都不匹配。

<root>
    <persons>
        <person>
            <lastname>doe</lastname>
        </person>    
        <person>
            <lastname>done</lastname>
        </person>    
    </persons>
    <persons>
        <person>
            <lastname>name</lastname>
        </person>    
        <person>
            <lastname>name</lastname>
        </person>    
    </persons>
</root>

在第二个列表中,<lastname>name</lastname>始终相等。但是在第一个列表中,姓氏有所不同。因此,我希望后面的xslt将输出doe。但事实并非如此:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

    <xsl:template match="/root">
        <xsl:for-each select="persons">
            <xsl:variable name="lastname" select="person[1]/lastname"/>
            <xsl:if test="not(person/lastname = $lastname)">
                <xsl:text>lastnames should be the same: </xsl:text>
                <xsl:value-of select="$lastname"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

旁注:我不想检测出哪个人的姓氏完全不同。我只想查找人员列表中的任何人

因此问题也可以改写为:如何将所有<lastname>提取为Set并检查是否set.size != 1

z2h5a1n5g 回答:如何针对xslt中的特定值检查所有listnode字段?

听起来count(distinct-values(person/lastname)) = 1用措词表达了您的状况。

,
  

我不想检测出哪个人的姓氏完全不同。我只想了解人员列表中是否有其他人员。

您可以执行简单的不等式检查(来自persons的上下文):

<xsl:if test="person/lastname != person/lastname">

如果任何person的{​​{1}}与同级的兄弟不同,则返回true: https://xsltfiddle.liberty-development.net/94AbWB6/5

本文链接:https://www.f2er.com/3159550.html

大家都在问