Xml XSLT:如果没有一个选中的值与我想要打印的值匹配,如何打印一个值?

Xml XSLT:如果没有一个选中的值与我想要打印的值匹配,如何打印一个值?,xml,xslt,if-statement,foreach,Xml,Xslt,If Statement,Foreach,XML: 名字3 名称1 姓名2 名称1 姓名2 我需要检查所有的“givenName”元素,如果它们的值与“person”元素中的“callingName”元素匹配。空值不应被视为匹配项。如果所有值都不匹配,则应打印“callingName”元素的值。我怎么做 以下是我迄今为止所尝试的: <person> <callingName>Name3</callingName> <givenNames> <giv

XML:


名字3
名称1
姓名2
名称1
姓名2
我需要检查所有的“givenName”元素,如果它们的值与“person”元素中的“callingName”元素匹配。空值不应被视为匹配项。如果所有值都不匹配,则应打印“callingName”元素的值。我怎么做

以下是我迄今为止所尝试的:

<person>
    <callingName>Name3</callingName>
    <givenNames>
        <givenName>Name1</givenName>
        <givenName>Name2</givenName>
        <givenName></givenName>
    </givenNames>
</person>
<person>
    <callingName></callingName>
    <givenNames>
        <givenName>Name1</givenName>
        <givenName>Name2</givenName>
        <givenName></givenName>
    </givenNames>
</person>


问题是每次值不匹配时,它都会打印'callingName'元素的值。如何检查所有的值是否都不匹配,值只打印一次?

以下XSLT匹配每个
,如果没有匹配的
givenName
,则在输出中包含
调用名

<xsl:if test="callingName != givenNames/givenName[normalize-space()]">
    <xsl:for-each select="givenNames/givenName[normalize-space()]">
        <xsl:element name="callingname">
            <xsl:value-of select="callingName"/>
        </xsl:element>
    </xsl:for-each>
</xsl:if>

结果:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="person">
    <xsl:if test="not(givenNames/givenName[text() = ../../callingName])">
        <xsl:copy-of select="callingName"/>
    </xsl:if>
</xsl:template>
</xsl:stylesheet>
Name3
如果没有匹配的值,“callingName”元素的值应该 被打印出来

尝试:

XSLT1.0

<callingName>Name3</callingName>
<callingName/>

应用于格式良好的(!)输入:

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

<xsl:template match="/">
    <output>
        <xsl:for-each select="people/person[string(callingName) and not(callingName=givenNames/givenName)]">
            <xsl:copy-of select="callingName" />
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

名字3
名称1
姓名2
名称1
姓名2
产生以下结果:

<people>
    <person>
        <callingName>Name3</callingName>
        <givenNames>
            <givenName>Name1</givenName>
            <givenName>Name2</givenName>
            <givenName></givenName>
        </givenNames>
    </person>
    <person>
        <callingName></callingName>
        <givenNames>
            <givenName>Name1</givenName>
            <givenName>Name2</givenName>
            <givenName></givenName>
        </givenNames>
    </person>
</people>

名字3
<?xml version="1.0" encoding="UTF-8"?>
<output>
   <callingName>Name3</callingName>
</output>