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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:26:37+00:00 2026-05-30T23:26:37+00:00

I want to: select all attributes foo, which are string values, and store the

  • 0

I want to:

  1. select all attributes “foo”, which are string values, and store the values in a list.
  2. transform each value of attribute “foo” in this list using some map in my xslt to a number.
  3. select the max value of the list and output that.

So given the following xml:

<t>
    <tag foo="A">apples</tag>
    <tag foo="C">oranges</tag>
    <tag foo="B">trees</tag>
</t>

And the following mapping:

<xsl:variable name="myMap">
    <entry key="A">1</entry>
    <entry key="B">2</entry>
    <entry key="C">3</entry>
</xsl:variable>

The output would be:

<max>3</max>

Another question, why can’t I indent my code? I’m putting spaces but it’s not working.

  • 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-30T23:26:38+00:00Added an answer on May 30, 2026 at 11:26 pm

    I This standard XSLT 1.0 transformation (most resembling your approach):

    <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:variable name="vrtfMap">
      <entry key="A" value="1"/>
      <entry key="B" value="2"/>
      <entry key="C" value="3"/>
      <entry key="X" value="8"/>
     </xsl:variable>
    
     <xsl:variable name="vMap" select=
      "document('')/*/xsl:variable[@name = 'vrtfMap']/*"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="@foo">
      <xsl:attribute name="foo">
       <xsl:value-of select="$vMap[@key = current()]/@value"/>
      </xsl:attribute>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the following XML document (as you didn’t provide any):

    <t foo="X">
        <a foo="A">
            <b foo="B"/>
        </a>
        <c foo="C"/>
    </t>
    

    produces the wanted, correct result:

    <t foo="8">
       <a foo="1">
          <b foo="2"/>
       </a>
       <c foo="3"/>
    </t>
    

    Explanation: Appropriate use of the XSLT current() function.


    II. XSLT 1.0 solution using keys for speed

    <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:key name="kValFromKey" match="entry/@value" use="../@key"/>
    
     <xsl:variable name="vrtfMap">
      <entry key="A" value="1"/>
      <entry key="B" value="2"/>
      <entry key="C" value="3"/>
      <entry key="X" value="8"/>
     </xsl:variable>
    
     <xsl:variable name="vMap" select=
      "document('')/*/xsl:variable[@name = 'vrtfMap']/*"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="@foo">
      <xsl:variable name="vCur" select="."/>
    
      <xsl:attribute name="foo">
       <xsl:for-each select="document('')">
        <xsl:value-of select="key('kValFromKey', $vCur)"/>
       </xsl:for-each>
      </xsl:attribute>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the same XML document (above), the same correct result is produced.

    Explanation:

    1. Use of <xsl:for-each select="document('')"> to set the current document to the stylesheet, so that the key() function will use the key index built for this document.

    2. Saving the node matched by the template in a variable so that we can use it inside the xsl:for-each — current() cannot be correctly used here, because it gets the current node on which xsl:for-each operates.

    UPDATE: The OP has now clarified in a comment that his biggest problem is finding the maximum.

    <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:variable name="vrtfMap">
          <entry key="A" value="1"/>
          <entry key="B" value="2"/>
          <entry key="C" value="3"/>
         </xsl:variable>
    
         <xsl:variable name="vMap" select=
          "document('')/*/xsl:variable[@name = 'vrtfMap']/*"/>
    
         <xsl:template match="/">
         <xsl:variable name="vDoc" select="."/>
    
          <xsl:variable name="vFoosMapped"
               select="$vMap[@key = $vDoc/*/*/@foo]"/>
          <max>
           <xsl:value-of select=
             "$vFoosMapped
                [not($vFoosMapped/@value > @value)]
                  /@value
             "/>
          </max>
         </xsl:template>
    </xsl:stylesheet>
    

    When given this XML document (the one provided by the OP lacks a singlr top element):

    <t>
        <tag foo="A">apples</tag>
        <tag foo="C">oranges</tag>
        <tag foo="B">trees</tag>
    </t>
    

    the wanted, correct result is produced:

    <max>3</max>
    

    Remark: A more efficient way of calculating maximum (or minimum — in a similar way) in XSLT 1.0 is to do this:

    <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:variable name="vrtfMap">
          <entry key="A" value="1"/>
          <entry key="B" value="2"/>
          <entry key="C" value="3"/>
         </xsl:variable>
    
         <xsl:variable name="vMap" select=
          "document('')/*/xsl:variable[@name = 'vrtfMap']/*"/>
    
         <xsl:template match="/">
         <xsl:variable name="vDoc" select="."/>
    
          <xsl:variable name="vFoosMapped"
               select="$vMap[@key = $vDoc/*/*/@foo]"/>
          <max>
           <xsl:for-each select="$vFoosMapped">
             <xsl:sort select="@value" data-type="number" order="descending"/>
    
             <xsl:if test="position() = 1">
              <xsl:value-of select="@value"/>
             </xsl:if>
           </xsl:for-each>
          </max>
         </xsl:template>
    </xsl:stylesheet>
    

    Another question, why can’t I indent my code? I’m putting spaces but
    it’s not working.

    This is a SO bug that they failed to fix for many months.

    Most probably you are using IE. If your version is 9, then do the following:

    1. Press F12.

    2. In the window that pops up click on the right-most menu and select: “Document mode: IE9 Standards”

    Now you should be able to see the code with indentation.

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

Sidebar

Related Questions

This is probably pretty simple. I want to select all elements of a given
I want to use simplified select one tag, which would generate select items list
I want to select all categories from a webservice. The webservice does not have
I want to select all the children of the body element before element with
Suppose I have a column called fruits I want to select all of the
I have two table emptable1(empid,status) emptable2(empid,week) i want to select all the empid whose
Below is my stored procedure. I want use stored procedure select all row of
a want that, he found all select elements with id than begins with 'chbTypes'
I want to write a query to select from a table all rows with
I have a class Foo which has_many Bars. Foo has an attribute, some_id. I

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.