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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:28:52+00:00 2026-06-05T20:28:52+00:00

I am working on XSLT, where I need to implement something as follows. My

  • 0

I am working on XSLT, where I need to implement something as follows.
My Source XML sample looks like this.

<?xml version="1.0" encoding="ISO-8859-1"?>
    <catalog>
        <cd>
            <title>A</title>  
            <title>B</title>
            <title>C</title>  
        </cd>
    </catalog>

Consider there is some key value pair list is there.

    Key         Value
    A           Algebra
    B           Biology
    C           Chemistry
    D           Data Analysis
    ---         ---

    ----        ---

I need to write an xslt such that for every occurance of key ‘A’, need to replace with appropriate value.

I also need to mention the list of Key value pairs in the same XSLT.
Sample Output:

<Data>
    <Subject>Algebra</Subject>
    <Subject>Biology</Subject>
    <Subject>Chemistry</Subject>
 </Data>

Can any one help me out how to do it.

Thank you.

  • 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-05T20:28:54+00:00Added an answer on June 5, 2026 at 8:28 pm

    I. Simple XSLT 1.0 Solution

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:my="my:my">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <my:codes>
       <code key="A" value="Algebra"/>
       <code key="B" value="Biology"/>
       <code key="C" value="Chemistry"/>
       <code key="D" value="Data Analysis"/>
     </my:codes>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
      "title/text()[. = document('')/*/my:codes/*/@key]">
    
      <xsl:value-of select=
       "document('')/*/my:codes/*[@key=current()]/@value"/>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <catalog>
        <cd>
            <title>A</title>
            <title>B</title>
            <title>C</title>
        </cd>
    </catalog>
    

    produces the wanted, correct result:

    <catalog>
       <cd>
          <title>Algebra</title>
          <title>Biology</title>
          <title>Chemistry</title>
       </cd>
    </catalog>
    

    Explanation:

    This is the standard way of including inline XML node as a global element (child element of xsl:stylesheet) that belongs to a (non-empty) namespace, different than the xsl namespace.


    II. More efficient XSLT 1.0 solution, using keys:

    <xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:my="my:my">
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
         <xsl:strip-space elements="*"/>
    
         <my:codes>
           <code key="A" value="Algebra"/>
           <code key="B" value="Biology"/>
           <code key="C" value="Chemistry"/>
           <code key="D" value="Data Analysis"/>
         </my:codes>
    
         <xsl:key name="kCodeByName" match="code" use="@key"/>
    
         <xsl:template match="node()|@*">
          <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
          </xsl:copy>
         </xsl:template>
    
         <xsl:template match=
          "title/text()[. = document('')/*/my:codes/*/@key]">
    
          <xsl:variable name="vCur" select="."/>
    
          <xsl:for-each select="document('')">
              <xsl:value-of select=
               "key('kCodeByName', $vCur)/@value"/>
          </xsl:for-each>
         </xsl:template>
    </xsl:stylesheet>
    

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

    <catalog>
       <cd>
          <title value="Algebra"/>
          <title value="Biology"/>
          <title value="Chemistry"/>
       </cd>
    </catalog>
    

    Explanation:

    Accessing data via the key() function is typically sub-linear — often O(1) and is extremely faster than linear search (which is important if the number of nodes to be searched is big).

    Accessing a node of one document via an index (xsl:key) while processing a node of another document is possible if the document containing the node to be looked-up is the current document. To access nodes from the other document, its root (or node of interest need to be saved and referenced off a variable.)


    III. XSLT 2.0 solution:

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:my="my:my">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:variable name="vCodes">
      <codes>
       <code key="A" value="Algebra"/>
       <code key="B" value="Biology"/>
       <code key="C" value="Chemistry"/>
       <code key="D" value="Data Analysis"/>
      </codes>
     </xsl:variable>
    
     <xsl:key name="kCodeByName" match="code" use="string(@key)"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
      "title/text()[key('kCodeByName', ., $vCodes)]">
    
      <xsl:sequence select=
       "key('kCodeByName', ., $vCodes)/@value"/>
     </xsl:template>
    </xsl:stylesheet>
    

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

    <catalog>
       <cd>
          <title value="Algebra"/>
          <title value="Biology"/>
          <title value="Chemistry"/>
       </cd>
    </catalog>
    

    Explanation:

    Almost the same as the efficient XSLT 1.0 solution, but:

    1. In XSLT 2.0 a template match pattern can contain a variable reference.

    2. In XSLT 2.0 there is no need for acrobatic tricks manipulating the current and the indexed documents — the 3rd argument of the key() function is to specify the tree whose index to use.

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

Sidebar

Related Questions

I am working on XSLT. Source XML: <?xml version=1.0 encoding=iso-8859-1?> <Content> <alertHeader> <ol xmlns=http://www.w3.org/1999/xhtml>
I am working on this from xslt cookbook type my.xml <?xml version=1.0 encoding=UTF-8?> <people>
I am working XSLT where the source looks like this. Source: <Data> <AB>all</AB> <AB>all2</AB>
I am working with an open source version of the Saxon XSLT processor Saxon
i have the following xslt sheet: <?xml version=1.0 encoding=UTF-8 ?> <xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform version=1.0> <xsl:variable
I'd like to get a real XSLT processor working with erlang. Which would be
I'm working on a XSLT transformation that consists on merging two XML and then
XSLT available is 1.0. I'm working on a dual-language site in an XML-based CMS
I'm working on XSLT 1.0 version I have declared an int variable that have
I'm working on an XSLT file to transform an XML doc that represents a

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.