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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:10:22+00:00 2026-05-22T01:10:22+00:00

(edited to include suggestions from Martin Honnen) Hello All, I’ve been trying to get

  • 0

(edited to include suggestions from Martin Honnen)

Hello All,

I’ve been trying to get the key function to work correctly in the stylesheet below.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:date="http://www.oracle.com/XSL/Transform/java/java.util.Date"
        xmlns:fo="http://www.w3.org/1999/XSL/Format"
        xmlns:m="http://mapping.tables" >


<xsl:key name="preferences" match="preferences/preference" use="@code"/>

<xsl:template match="Reservation/Detail" >
    <xsl:for-each select="Preferences/Preference">
        <xsl:if test ="string-length(./PreferenceCode)>0">

            &#160;&#160;&#160;&#160;* (<xsl:value-of select="./PreferenceCode"/>)

            <xsl:choose>
                <xsl:when test="./PreferenceCode!='PETS'">

                    <xsl:call-template name="prefmap">
                            <xsl:with-param name="code" select="./PreferenceCode"/>
                    </xsl:call-template>

                    <br/><br/>
                </xsl:when>
            </xsl:choose>
        </xsl:if>
    </xsl:for-each>
</xsl:template


<xsl:template name="prefmap">
        <xsl:param name="code"/>
        You got here (called template) with code <xsl:value-of select="$code"/>
              <xsl:for-each select="document('')">
        <xsl:value-of select="key('preferences',$code)"/>
    </xsl:for-each>
</xsl:template>

<m:Maps xmlns="">
<preferences>
        <preference code="ANT">
                Hypoallergenic Bedding
        </preference>
        <preference code="NSK">
                Non-smoking Room
        </preference>
        <preference code="SMK">
                Smoking Room
        </preference>
</preferences>
</m:Maps>

</xsl:stylesheet>

It takes a input (that I don’t control) and produces this:

* (ANT) You got here (called template) with code ANT


* (EARLY) You got here (called template) with code EARLY


* (NSK) You got here (called template) with code NSK

When I was expecting:

* (ANT) You got here (called template) with code ANT
  Hypoallergenic Bedding

* (EARLY) You got here (called template) with code EARLY

* (NSK) You got here (called template) with code NSK
Non-smoking Room

I tried to include this snippet in the main template to debug, but it produced no output:

            <xsl:for-each select="key('preferences',./PreferenceCode)">
                <p>
                    Code: <xsl:value-of select="@code"/><br />
                    Description: <xsl:value-of select="."/>
                </p>
            </xsl:for-each>

Do I have a problem with my key definition or the way that I am trying to use it?

Thanks all 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-22T01:10:23+00:00Added an answer on May 22, 2026 at 1:10 am

    Do I have a problem with my key
    definition or the way that I am trying
    to use it?

    There is no problem with any of them:

    I cannot reproduce the reported output. This means that if there really is any problem, it is in the code not shown to us.

    The provided transformation:

    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:date="http://www.oracle.com/XSL/Transform/java/java.util.Date"
      xmlns:fo="http://www.w3.org/1999/XSL/Format"
      xmlns:m="http://mapping.tables" >
    
        <xsl:key name="preferences" match="preferences/preference" use="@code"/>
    
        <xsl:template match="Reservation/Detail" >
            <xsl:for-each select="Preferences/Preference">
                <xsl:if test ="string-length(./PreferenceCode)>0">              &#160;&#160;&#160;&#160;* (
                    <xsl:value-of select="./PreferenceCode"/>)              
                    <xsl:choose>
                        <xsl:when test="./PreferenceCode!='PETS'">
                            <xsl:call-template name="prefmap">
                                <xsl:with-param name="code" select="./PreferenceCode"/>
                            </xsl:call-template>
                            <br/>
                            <br/>
                        </xsl:when>
                    </xsl:choose>
                </xsl:if>
            </xsl:for-each>
        </xsl:template>
        <xsl:template name="prefmap">
            <xsl:param name="code"/>         You got here (called template) with code 
            <xsl:value-of select="$code"/>
            <xsl:for-each select="document('')">
                <xsl:value-of select="key('preferences',$code)"/>
            </xsl:for-each>
        </xsl:template>
        <m:Maps xmlns="">
            <preferences>
                <preference code="ANT">
                  Hypoallergenic Bedding
                </preference>
                <preference code="NSK">
                 Non-smoking Room
                </preference>
                <preference code="SMK">
                 Smoking Room
                </preference>
            </preferences>
        </m:Maps>
    </xsl:stylesheet>
    

    when applied on the following XML document (no XML document was provided in the question!):

    <Reservation>
        <Detail>
            <Preferences>
                <Preference>
                  <PreferenceCode>ANT</PreferenceCode>
                </Preference>
                <Preference>
                  <PreferenceCode>NSK</PreferenceCode>
                </Preference>
                <Preference>
                  <PreferenceCode>SMK</PreferenceCode>
                </Preference>
                <Preference>
                  <PreferenceCode>PETS</PreferenceCode>
                </Preference>
            </Preferences>
        </Detail>
    </Reservation>
    

    produces exactly the expected result:

    <?xml version="1.0" encoding="utf-8"?>
                          * (
                    ANT)              
                             You got here (called template) with code 
            ANT
                  Hypoallergenic Bedding
                <br xmlns="http://www.w3.org/1999/xhtml" xmlns:date="http://www.oracle.com/XSL/Transform/java/java.util.Date" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:m="http://mapping.tables"/><br xmlns="http://www.w3.org/1999/xhtml" xmlns:date="http://www.oracle.com/XSL/Transform/java/java.util.Date" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:m="http://mapping.tables"/>                  * (
                    NSK)              
                             You got here (called template) with code 
            NSK
                 Non-smoking Room
                <br xmlns="http://www.w3.org/1999/xhtml" xmlns:date="http://www.oracle.com/XSL/Transform/java/java.util.Date" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:m="http://mapping.tables"/><br xmlns="http://www.w3.org/1999/xhtml" xmlns:date="http://www.oracle.com/XSL/Transform/java/java.util.Date" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:m="http://mapping.tables"/>                  * (
                    SMK)              
                             You got here (called template) with code 
            SMK
                 Smoking Room
                <br xmlns="http://www.w3.org/1999/xhtml" xmlns:date="http://www.oracle.com/XSL/Transform/java/java.util.Date" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:m="http://mapping.tables"/><br xmlns="http://www.w3.org/1999/xhtml" xmlns:date="http://www.oracle.com/XSL/Transform/java/java.util.Date" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:m="http://mapping.tables"/>                  * (
                    PETS)              
    

    Do Note:

    All the following 7 XSLT processors produce exactly the same (above) result: MSXML3/4, XslCompiledTransform, XslTransform, Saxon 6.5.4, Saxon 9.1.05, AltovaXML (XmlSPY).

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

Sidebar

Related Questions

Its been a while now and im still trying to get a certain code
// edited by Neil Butterworth to conserve vertical space #include <stdio.h> struct A; struct
edited, this is it function handleDate( timestamp ) { var n=new Date(), t, ago
What gives the best performance? (edited to include another option) var myVec:Vector.<Number> = new
My question relates to how large websites, edited by several hundred people who work
I am trying to compile a code that has a malloc function inside the
so I have a file that will get $_GET['id'] from an ajax request here
I edited the apache httpd.conf file recently for the mod_rewrite to work. I don't
Essentially, what I'm trying to do is to load/draw a map from a Tiled
so i am currently doing this... //Codeblock edited to include otaku upgrade const String

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.