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

  • SEARCH
  • Home
  • 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 6874179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:09:45+00:00 2026-05-27T04:09:45+00:00

The sample presented is a simplified version of what I am trying to do.

  • 0

The sample presented is a simplified version of what I am trying to do.

Using XSLT, how do I compare data from nodes in different (non -child) xpaths? I want to compare each cd/artist with the dvd/director to see if they match (see samples below). There are multiple cd’s and dvd’s, but in this simplified example I only used one of each. I tried doing the comparison within one template, but I couldn’t access the dvd xpath. And I couldn’t access the dvd xpath by using a separate template. When using a separate template, I created a ‘compare’ template that I called (<xsl:call-template name="compare">). By access, I mean that I couldn’t print out the dvd/director’s value.

My Question: How do I access one xpath when accessing a different xpath? What I want to do is compare cd artist with dvd director to see if they match. And print out these values, if they match. What I can’t figure out is how to get the artist and director so I can compare them. My xslt does print the cd’s artist value, but I can’t print out the dvd/director’s value.

My samples follows:

The samples come from W3Schools. I modified them to illustrate my question.

Sample XSLT

<xsl:for-each select="catalog/cd">
   <xsl:value-of select="@artist"/>
    <xsl:for-each select="catalog/dvd">         
    <xsl:if test="@director = @artist">
        <xsl:value-of select="@director"/>
    </xsl:if>           
   </xsl:for-each>
 </xsl:for-each>

Sample XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>   
<cd title="Unchain my heart" artist="Joe Cocker">

    <country>USA</country>
    <company>EMI</company>
    <price>8.20</price>
    <year>1987</year>
</cd>

    <dvd title="My DVD Movie" director="Joe Cocker">
    <country>USA</country>
    <company>WB</company>
    <price>20.00</price>
    <year>1999</year>
    </dvd>
</catalog>

Thanks for your help in advance.

  • 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-05-27T04:09:46+00:00Added an answer on May 27, 2026 at 4:09 am

    I think the larger issue is that you’re thinking in procedural terms, which is rarely the right way to approach a problem with XSLT. A better way is to first match cd elements and then select the dvd elements whose director matches the artist. Consider this stylesheet:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:template match="/*/cd">
            <xsl:value-of select="artist"/>
            <xsl:apply-templates select="../dvd[director=current()/artist]"
                mode="out"/>
        </xsl:template>
        <xsl:template match="dvd" mode="out">
            <xsl:value-of select="director"/>
        </xsl:template>
        <xsl:template match="dvd"/>
    </xsl:stylesheet>
    

    The first template applies templates to all dvd elements for which the following predicate evaluates to true: director=current()/artist

    Of course, as in your example, we’re just printing the name Joe Cocker twice, which isn’t very useful.

    Note: This approach breaks down when there is more than one cd element with the same artist. More complicated grouping strategies would address that case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to get familiar with spatial data using sql server 2008. I use
I am trying to build a simple booking application for a school presented as
Sample code that shows how to create threads using MFC declares the thread function
Sample data: !!Part|123456,ABCDEF,ABC132!! The comma delimited list can be any number of any combination
Sample Data: 603 Some garbage data not related to me, 55, 113 -> 1-ENST0000
I'm trying to test a Presenter created using ASP.NET WebFormsMVP. I'm building it using
I am trying to get a sample program working with JUNG , a graphing
I'm trying to put a simple UIToolbar at the bottom of a modally presented
I have extended DotNetOpenAuth ASP.NET MVC Login sample while trying to get at least
I am using djangos built in comments framework presented in this example: https://docs.djangoproject.com/en/1.3/ref/contrib/comments/example/ And

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.