Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:43:30+00:00 2026-06-09T13:43:30+00:00

So I’m having trouble understanding why one XPath expression gets the nodes I want,

  • 0

So I’m having trouble understanding why one XPath expression gets the nodes I want, while the other doesn’t.

First, the xml:

<doc>
    <source id="225" clientID="567" matterID="225" level="2" />
    <source id="226" clientID="993" matterID="226" level="2" />
    <dest id="185" level="7" />
    <dest id="226" level="7" />
</doc>

The keys in my xsl template are defined as follows:

<xsl:key name="sourceId" match="//source" use="@id" />
<xsl:key name="destId" match="//dest" use="@id" />
<xsl:key name="destLevel" match="//dest" use="@level" />

What I’m looking for are the source nodes, that match dest nodes on id, but have a different level attribute. The apply template that I figured would work in my head is the following:

<xsl:apply-templates select="source[key('destId', @id) and not(key('destLevel', @level))]" mode="update" />

But that doesn’t seem to work. A colleague suggested putting a not around an expression that matches the nodes I don’t want, and after a lot of trial and error, I thought this might work, to no effect:

<xsl:apply-templates select="source[not(not(key('destId', @id)) or not(key('destLevel', @level)))]" mode="update" />

Can anyone please walk me through what I need in order to solve this?

Edit: I previously thought I’d solved this with the second query, but it seems I was mistaken.

====Solution====

Dimitre Novatchev has a detailed breakdown of different ways to solve this, but my ultimate solution was actually slightly different than his.

In essence, I created a virtual key with the concat() function that combined the two attributes. That way, I could find nodes that matched the id, but not the id-level combo.

Extra key:

<xsl:key name="destByIdAndLevel" match="//dest" use="concat(@id,'+',@level)" />

Changed apply-template call:

<xsl:apply-templates select="source[key('destId', @id) and not(key('destByIdAndLevel',concat(@id,'+',@level)))]" mode="update" /> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:43:32+00:00Added an answer on June 9, 2026 at 1:43 pm

    I. This transformation:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="source">
       <xsl:copy-of select=
         "self::*[../dest[@id = current()/@id and not(@level=current()/@level)]]"/>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on this XML document (an additional source element added to the provided XML document — to verify more cases):

    <doc>
     <source id="185" clientID="567" matterID="225" level="7" />
     <source id="225" clientID="567" matterID="225" level="2" />
     <source id="226" clientID="993" matterID="226" level="2" />
     <dest id="185" level="7" />
     <dest id="226" level="7" />
    </doc>
    

    produces the wanted, correct result:

    <source id="226" clientID="993" matterID="226" level="2"/>
    

    II. Solution using one key:

    <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="kDestById" match="dest" use="@id"/>
    
     <xsl:template match="/*">
      <xsl:copy-of select=
       "source[key('kDestById',@id)
             and
               not(@level=key('kDestById',@id)/@level)]"/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the same XML document (above), again the same wanted, correct result is produced:

    <source id="226" clientID="993" matterID="226" level="2"/>
    

    III. Solution with two keys:

    <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="kDestById" match="dest" use="@id"/>
     <xsl:key name="kDestByLevel" match="dest" use="@level"/>
    
     <xsl:template match="source">
      <xsl:copy-of select=
       "self::*
            [key('kDestById',@id)
           and
             key('kDestById',@id)
                 [not(count(.|key('kDestByLevel',current()/@level))
                     =
                      count(key('kDestByLevel',current()/@level))
                     )
                 ]
            ]"/>
     </xsl:template>
    </xsl:stylesheet>
    

    This again produces the wanted, correct result:

     <source id="226" clientID="993" matterID="226" level="2"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm making a simple page using Google Maps API 3. My first. One marker
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.