Xml 将一个节点与另一个节点进行比较的xsl交叉引用

Xml 将一个节点与另一个节点进行比较的xsl交叉引用,xml,xslt,xpath,Xml,Xslt,Xpath,我有一个如下所示的xml片段 <xml> <person> <name>bob</name> <holidays> <visit>GB</visit> <visit>FR</visit> </holidays> </person> <person> <name>joe</n

我有一个如下所示的xml片段

<xml>
<person>
    <name>bob</name>
    <holidays>
        <visit>GB</visit>
        <visit>FR</visit>
    </holidays>
</person>
<person>
    <name>joe</name>
    <holidays>
        <visit>DE</visit>
        <visit>FR</visit>
    </holidays>
</person>


<countrylist>
    <country>GB</country>
    <country>FR</country>
    <country>DE</country>
    <country>US</country>
</countrylist>
</xml>
以下是我迄今为止所尝试的:

<xsl:template match="xml">
    <xsl:apply-templates select="person">
    </xsl:apply-templates>
</xsl:template>


<xsl:template match="person">
<xsl:value-of select="name"></xsl:value-of>
    <xsl:apply-templates select="holidays"></xsl:apply-templates>
</xsl:template>

<xsl:template match="holidays">
            <xsl:variable name="v" select="holidays"></xsl:variable>
    <xsl:for-each select="/xml/countrylist/country">
        <xsl:variable name="vcountry" select="."></xsl:variable>
        <xsl:if test="$v/holidays[$vcountry]">      
        <xsl:value-of select="$vcountry"></xsl:value-of><xsl:value-of select="'*'"/>
        </xsl:if>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

编辑:我最终使用下面的命令进行管理;有没有更简单的方法

<xsl:template match="xml">
    <xsl:apply-templates select="person">
    </xsl:apply-templates>
</xsl:template>
<xsl:template match="person">
    <xsl:variable name="hols" select="holidays"/>
    <xsl:value-of select="name"/>
    <xsl:for-each select="/xml/countrylist/country">
        <xsl:variable name="vcountry" select="."/>
        <xsl:if test="$hols[visit=$vcountry]">
            <xsl:value-of select="$vcountry"/>
            <xsl:value-of select="'*'"/>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

如果您只想显示每个人访问过的国家(而不想显示他们没有访问过的国家的“否”),那么您根本不需要涉及
国家列表

<xsl:template match="person">
    <xsl:value-of select="name"/>
    <xsl:for-each select="holidays/visit">
        <xsl:value-of select="." />
        <xsl:text> *</xsl:text>
    </xsl:for-each>
</xsl:template>

*
如果您确实想要“否”条目,那么您的方法很好,但您可以将其简化一点:

<xsl:template match="person">
    <xsl:variable name="visits" select="holidays/visit"/>
    <xsl:value-of select="name"/>
    <xsl:text> - </xsl:text>
    <xsl:for-each select="/xml/countrylist/country">
        <xsl:value-of select="." />
        <xsl:choose>
            <xsl:when test=". = $visits">
                <xsl:text>: Yes  </xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>: No  </xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
</xsl:template>

- 
:是的
:没有

如果左侧的节点(本例中为
国家/地区
)与右侧的任何节点(当前人员的
访问
元素)具有相同的值,则利用节点与节点集之间的相等比较为真这一事实。

非常好……感谢Ian。这是一个很好的建议@dmckinney当两侧都有一个节点集时,同样的情况也会扩展到比较——如果左侧集合中的任何节点与右侧集合中的任何节点匹配,则比较成功。这就是为什么
x!=y
not(x=y)
可以给出不同的结果-第一个意思是“至少有一对x和y不匹配”,而第二个意思是“没有一对x和y不匹配”。