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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:37:24+00:00 2026-05-31T04:37:24+00:00

I need to search for a range of elements based on ID numbers. If

  • 0

I need to search for a range of elements based on ID numbers. If the ID number is found, I’ll process it. If the ID is not found, it’ll get filled in with a default value. I’m using XSLT 2.0 in Saxon 9.4 HE.

Here’s a very simplified example. The input XML looks like this:

<root>
    <item>
        <id>1</id>
    </item>
    <item>
        <id>2</id>
    </item>
    <item>
        <id>4</id>
    </item>
</root>

If I’m searching for items whose ID is 1, 2, or 3, I’d like to get the following output. Note that I don’t want any output for item 4.

Found 1
Found 2
Didn't find 3

My first attempt was with a for-each loop but it doesn’t even compile. The error I get is “XPTY0020: Axis step child::element(”:id) cannot be used here: the context item is an atomic value” from the line “”.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:variable name="ids" select="(1, 2, 3)"/>

<xsl:template match="/root">
    <xsl:for-each select="$ids">
        <xsl:choose>
            <xsl:when test="count(item/id = .) > 0">
                Found <xsl:value-of select="."/>
            </xsl:when>
            <xsl:otherwise>
                Didn't find <xsl:value-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
</xsl:template> 

</xsl:stylesheet>

After that I realized I could easily find the matching ones with the following, but I can’t puzzle out a way to find the missing ones.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:variable name="ids" select="(1, 2, 3)"/>

<xsl:template match="/">
    <xsl:apply-templates select="root/item[id=$ids]"/>
</xsl:template> 

<xsl:template match="item">
    Found <xsl:value-of select="id"/>
</xsl:template> 

</xsl:stylesheet>

BTW, the output also needs to be sorted by ID, which means that dumping a list of all the items that were found, followed by a list of the ones that were not found won’t work. Is this even possible or should I take the easy/wimpy road and change my input?

  • 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-31T04:37:26+00:00Added an answer on May 31, 2026 at 4:37 am

    Here is an example:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    
    <xsl:key name="k1" match="item" use="number(id)"/>
    
    <xsl:param name="ids" select="(1, 2, 3)"/>
    
    <xsl:template match="/">
        <xsl:variable name="root" select="."/>
        <xsl:for-each select="$ids">
          <xsl:choose>
            <xsl:when test="key('k1', ., $root)">
              <xsl:text>Found </xsl:text>
              <xsl:value-of select="."/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>Didn't find </xsl:text>
              <xsl:value-of select="."/>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template> 
    
    </xsl:stylesheet>
    

    [edit]
    Your initial attempt was right as for as processing the sequence of ids is concerned, but you need to store the main input document outside of the for-each as inside the context item is an id number value and without the outer variable you wouldn’t be able to access the item elements in the document.

    And for efficiency, I added the key.

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

Sidebar

Related Questions

I have a multidimensional array. I need to search it for a specific range
I need to search across multiple columns from two tables in my database using
I need to perform google search to retrieve the number of results for a
I need to find a reliable method to search for Arabic words using PHP.
Problem I have timestamped data, which I need to search based on the timestamp
I need a structure to hold a value based on a key that has
I need to query my DB based on a range of two values which
I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount%
I need to search a string in the string array. I dont want to
I need to search for all users containing a certain text string in their

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.