【问题标题】:Not IN equivalent in XPath expression在 XPath 表达式中不等效
【发布时间】:2010-08-14 17:43:30
【问题描述】:

我正在进行 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 表达式。 (任何替代品也可以提供帮助)

【问题讨论】:

  • 好问题 (+1)。请参阅我的答案以获取高效、简短且简单的 XSLT 解决方案。 :)

标签: xslt xpath


【解决方案1】:

在 XPath 1.0 中:

/Message/Customers/Customer[not(@pin=/Message/Addresses/Address/@pin1)]

【讨论】:

  • @Saravanandss:你很高兴!
  • +1。这是基本的 XPath 知识——不幸的是,OP 很高兴“它有效”并且可能不会进一步深入了解实际发生的事情...... :(
  • 我认为@Dimitre 所指的基本情况是“=”运算符是“存在量化比较”运算符,即A = B 告诉您是否存在是一对值,一个在节点集 A 中,一个在节点集 B 中,它们彼此相等。在上面的例子中,A 是@pin,它只寻址一个节点(因为它在只有一个Customer 的上下文中),而 B 路径寻址许多节点。
【解决方案2】:

我赞成的@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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2017-10-12
    • 1970-01-01
    • 2018-05-03
    • 2022-10-03
    • 1970-01-01
    相关资源
    最近更新 更多