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 7570207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:21:49+00:00 2026-05-30T15:21:49+00:00

All, I’m new to XSLT and facing a typical problem. Below is the problem

  • 0

All,

I’m new to XSLT and facing a typical problem. Below is the problem details

Based on the data below, i needed to show ItemA/ANum element only when its of AName BBC and not part of ItemB/tempBNum

So desired result for the below data should be 0214,BBC, since its part of the ItemA, also is a BBC is does not appear in ItemB.

<myData>
<ItemA>
    <ANum>0213</ANum>
    <AName>OOC</AName>
</ItemA>
<ItemA>
    <ANum>0031</ANum>
    <AName>BBC</AName>
</ItemA>
<ItemA>
    <ANum>0214</ANum>
    <AName>BBC</AName>
</ItemA>
<ItemA>
    <ANum>0044</ANum>
    <AName>BBC</AName>
</ItemA>

<ItemB>
    <tempBNum>0031</tempBNum>
</ItemB>
<ItemB>
    <tempBNum>0044</tempBNum>
</ItemB>
</myData>

For this end, I tried putting in a logic of having Nested For Loops, only problem being nested loop should break when ItemAObj/ANum = tempBNum, but its not breaking and producing duplicate results.

  <xsl:for-each select="myData/ItemA">
    <xsl:variable name="ItemAObj" select="." />
    Num = <xsl:value-of select="$ItemAObj/ANum"/>
    <xsl:for-each select="/myData/ItemB">
    <xsl:choose>
        <xsl:when test="$ItemAObj/ANum!=tempBNum and $ItemAObj/AName='BBC'"> <xsl:value-of select="tempBNum"/>
        <xsl:text> </xsl:text>
    </xsl:when>
        </xsl:choose>
        </xsl:for-each>
  </xsl:for-each>

Any help on this will be greatly appreciated.

  • 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-30T15:21:50+00:00Added an answer on May 30, 2026 at 3:21 pm

    You don’t need to use any xsl:for-each to solve the problem, much more you don’t need nested xsl:for-each.

    In fact, the wanted ItemA elements can be selected with a single XPath expression:

    /*/ItemA[AName = 'BBC' 
           and 
             not(ANum = /*/ItemB/tempBNum)
            ]
    

    Therefore a simple XSLT transformation that evaluates the above XPath expression and copies the result to the output is:

    <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="/">
         <xsl:copy-of select=
         "/*/ItemA[AName = 'BBC' and not(ANum = /*/ItemB/tempBNum)]"/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied to the provided XML document:

    <myData>
        <ItemA>
            <ANum>0213</ANum>
            <AName>OOC</AName>
        </ItemA>
        <ItemA>
            <ANum>0031</ANum>
            <AName>BBC</AName>
        </ItemA>
        <ItemA>
            <ANum>0214</ANum>
            <AName>BBC</AName>
        </ItemA>
        <ItemA>
            <ANum>0044</ANum>
            <AName>BBC</AName>
        </ItemA>
        <ItemB>
            <tempBNum>0031</tempBNum>
        </ItemB>
        <ItemB>
            <tempBNum>0044</tempBNum>
        </ItemB>
    </myData>
    

    the wanted, correct result is produced:

    <ItemA>
       <ANum>0214</ANum>
       <AName>BBC</AName>
    </ItemA>
    

    If we want just the values of the text nodes, just use the string() function:

    string(/*/ItemA[AName = 'BBC' and not(ANum = /*/ItemB/tempBNum)])
    

    Then the so modified transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
         <xsl:copy-of select=
         "string(/*/ItemA[AName = 'BBC' and not(ANum = /*/ItemB/tempBNum)])"/>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the same XML document (above), produces:

        0214
        BBC
    

    Note: If performance starts to matter, using keys could be the next step to increase the efficiency of the transformation.

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

Sidebar

Related Questions

All, See the code below: function menu() { this.menuitem=[]; this.submenu=[]; this.menuitem[0] = $('div#sivname1'); this.menuitem[1]
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
All the examples of use and tutorials I have found on beginBackgroundTaskWithExpirationHandler: show how
All the day numbers and week days on twitter bootstrap date picker show up
All the code/commands below are part of a PHP script that processes images from
all I want to display last 5 entered data for specific id. My sql
All my classes are derived from this T class: public abstract class Problem<T, TResult>
All the articles I've found via google are either obsolete or contradict one another.
All the recent VisualSVN Server posts made me want to check it out. I
All front-end developers know the pain of coding for Firefox, then viewing our then

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.