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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:53:11+00:00 2026-05-28T15:53:11+00:00

I have a following XML file: <titles> <book title=XML Today author=David Perry/> <book title=XML

  • 0

I have a following XML file:

<titles>
   <book title="XML Today" author="David Perry"/>
   <book title="XML and Microsoft" author="David Perry"/>
   <book title="XML Productivity" author="Jim Kim"/>
   <book title="XSLT 1.0" author="Albert Jones"/>
   <book title="XSLT 2.0" author="Albert Jones"/>
   <book title="XSLT Manual" author="Jane Doe"/>
</titles>

I want to eliminate some elements and apply the following XSLT:

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:output method="xml" indent="yes"/>

  <xsl:key name="author1-search" match="book[starts-with(@author, 'David')]" use="@title"/>
  <xsl:template match="book [key('author1-search', @title)]" />

  <xsl:key name="author2-search" match="book[starts-with(@author, 'Jim')]" use="@title"/>
  <xsl:template match="book [key('author2-search', @title)]" />

  <xsl:template match="/">  
    <xsl:apply-templates />
  </xsl:template>

</xsl:stylesheet>

Is it possible to use an inline xsl variable

  <xsl:variable name="Author"> 
    <name>David</name> 
    <name>Jim</name> 
  </xsl:variable> 

instead of “author1-search”, “author2-search”, and so on to loop through names?

I can use only XSLT 1.0 (2.0 is currently not supported).

Thanks in advance,

Leo

  • 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-28T15:53:12+00:00Added an answer on May 28, 2026 at 3:53 pm

    No, patterns (in XSLT 1.0) cannot contain variable/parameter references.

    One way to perform such a task would be like this:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
    
     <xsl:param name="pAuthor" select="'David Perry'"/>
    
        <xsl:key name="kBookaByAuthor" match="book"
                 use="@author"/>
    
        <xsl:template match="/">
    
         Books written by <xsl:value-of select="$pAuthor"/> :<xsl:text/>
            <xsl:apply-templates
                 select="key('kBookaByAuthor', $pAuthor)"/>
        </xsl:template>
    
        <xsl:template match="book">
         <!-- One can do more formatting here -->
         <xsl:text>&#xA;     </xsl:text>
         <xsl:value-of select="@title"/>
        </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <titles>
        <book title="XML Today" author="David Perry"/>
        <book title="XML and Microsoft" author="David Perry"/>
        <book title="XML Productivity" author="Jim Kim"/>
        <book title="XSLT 1.0" author="Albert Jones"/>
        <book title="XSLT 2.0" author="Albert Jones"/>
        <book title="XSLT Manual" author="Jane Doe"/>
    </titles>
    

    the wanted, correct result is produced:

    Books written by David Perry :
      XML Today
      XML and Microsoft
    

    Update: In a comment the OP has clarified that:

    “I thought I fully specified my requirements in the initial question.
    As I mentioned in my question and in my first comment, it would be
    helpful to me to see the approach for dealing with more than one
    author
    “

    Here is a solution that truly uses keys (note that the “key” in the answer by @Flynn1179 doesn’t build any index and is just a constant sequence of strings– so the function key() using that xsl:key is actually finding a string in a list of strings — which is O(N) as contrasted to, typically, O(1) for searching in a true index):

    <xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:ext="http://exslt.org/common">
    
         <xsl:output method="text"/>
    
         <xsl:param name="pAuthors">
          <x>David Perry</x>
          <x>Jane Doe</x>
         </xsl:param>
    
         <xsl:variable name="vParams" select=
          "ext:node-set($pAuthors)/*"/>
    
         <xsl:key name="kBookByAuthor" match="book"
                         use="@author"/>
    
         <xsl:template match="/">
           Books written by : <xsl:text/>
    
           <xsl:apply-templates select="$vParams"/>
    
           <xsl:apply-templates select=
            "key('kBookByAuthor', $vParams)"/>
         </xsl:template>
    
         <xsl:template match="book">
           <!-- One can do more formatting here -->
           <xsl:text>&#xA;     </xsl:text>
           <xsl:value-of select="concat('&quot;', @title, '&quot;')"/>
         </xsl:template>
    
         <xsl:template match="x">
          <xsl:if test="not(position() = 1)">, </xsl:if>
          <xsl:value-of select="."/>
         </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied to the provided XML document (above), the wanted, correct result is produced:

       Books written by : David Perry, Jane Doe
         "XML Today"
         "XML and Microsoft"
         "XSLT Manual"
    

    Do note: In this solution the Exslt function node-set() is used. This is done only for convenience here. In a real usage, the value of the parameter will be specified externally and then the ext:node-set() function isn’t necessary.

    Efficiency: This solution uses the true power of keys in XSLT. An experiment made using MSXML (3, 4 and 6) XSLT processors shows that if we search for 10000 authors the transformation time with different XSLT processors ranges from: 32ms to 45ms.

    Interestingly, the solution presented by @Flynn1179 doesn’t indeed make key index and with many XSLT processors it takes (for the same number (10000) of authors) from 1044ms to 5564ms:

    MSXML3: 5564 ms.,

    MSXML4: 2526ms,

    MSXML6: 4867 ms,

    AltovaXML: 1044ms.

    This is quite inferior to the performance one gets with true key indexing (32ms to 45ms).

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

Sidebar

Related Questions

For example I have the following XML file: <?xml version=1.0 encoding=ISO-8859-1?> <bookstore> <book> <title
I have an xml file with the following data: <BOOKS> <BOOK> <TITLE image=./images/govnahDesign.jpg>Bible</TITLE> <CATEGORY>Book</CATEGORY>
I have books.xml file which contains author name and book titles. I am using
friends, i have created custom title bar using following titlebar.xml file with code <?xml
I have the following xml file <gallery> <album title=test description=test lgPath=/images/commphotos/> <img src=1130975173.jpg />
I have created an XML document with the following contents. <books> <book id=1> <title>Title01</title>
I have the following XML file: <Promotions> <Promotion> <Category>Arts &amp; Entertainment</Category> <Client>Client Five</Client> <Title>Get
I have the following XML file: <phonebook> <departments> <department id=1 parent= title=Rabit Hole address=
I have the following xml file in my res/raw folder - Title of RSS
Assume I have the following XML file: <Movies> <Movie ownerName=Ryan> <Title>The Lord of 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.