我正在进行一个XSL开发,我需要知道XPATH中的NOT in等效项。我将以所有人都能理解的最简单的格式介绍XML和XSL。
<?xml-stylesheet type="text/xsl" href="XSL.xsl"?>
<Message>
<Customers>
<Customer pin="06067">1</Customer>
<Customer pin="06068">2</Customer>
<Customer pin="06069">3</Customer>
<Customer pin="06070">4</Customer>
<Customer pin="06072">5</Customer>
</Customers>
<Addresses>
<Address pin1="06067">A</Address>
<Address pin1="06068">B</Address>
<Address pin1="06069">C</Address>
</Addresses>
</Message>XSL
<xsl:template match="/Message">
<html>
<body>
<h4>Existing Customers</h4>
<table>
<xsl:apply-templates select="//Customers/Customer[@pin = //Addresses/Address/@pin1]"></xsl:apply-templates>
</table>
<h4>New Customers</h4>
<table>
<!--This place need to be filled with new customers-->
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Customer" name="Customer">
<xsl:variable name="pin" select="./@pin"></xsl:variable>
<tr>
<td>
<xsl:value-of select="."/>
<xsl:text> is in </xsl:text>
<xsl:value-of select="//Addresses/Address[@pin1=$pin]"/>
</td>
</tr>
</xsl:template>
在上面的XSLT中,在注释区域下,我需要匹配并显示Addresses/Address节点中不存在地址的客户。
请帮助查找与不在地址节点集中的客户相匹配的XPath表达式。(任何替代方案也可以提供帮助)
发布于 2010-08-15 01:51:44
在XPath 1.0中:
/Message/Customers/Customer[not(@pin=/Message/Addresses/Address/@pin1)]发布于 2010-08-15 03:07:38
我对@Alejandro的好答案的另一个替代方案是下面的转换,它使用键,如果现有客户的数量是大的,将会更高效。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kexistingByPin"
match="Address" use="@pin1"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select=
"*/*/Customer[not(key('kexistingByPin', @pin))]"/>
</xsl:template>
</xsl:stylesheet>在所提供的XML文档上应用此转换时为
<Message>
<Customers>
<Customer pin="06067">1</Customer>
<Customer pin="06068">2</Customer>
<Customer pin="06069">3</Customer>
<Customer pin="06070">4</Customer>
<Customer pin="06072">5</Customer>
</Customers>
<Addresses>
<Address pin1="06067">A</Address>
<Address pin1="06068">B</Address>
<Address pin1="06069">C</Address>
</Addresses>
</Message>生成所需的正确答案
<Customer pin="06070">4</Customer>
<Customer pin="06072">5</Customer>https://stackoverflow.com/questions/3484431
复制相似问题