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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:17:43+00:00 2026-05-31T16:17:43+00:00

If have a big ‘xsl:choose’ chunk in which I need to set a number

  • 0

If have a big ‘xsl:choose’ chunk in which I need to set a number of defined sets of attributes on different elements.

I really do not like to repeat the definition of sets of attributes inside every branch of the ‘choose’.
So I would like to work with a variable that contains those attributes.
A lot easier to maintain and less room for error…

So far I have not been able to call the attribute node out?
I thought they are just a node-set, so copy-of would do the trick.
But that gives me nothing on output.
Is this because attribute nodes are not really children?
But XSLT 1.O does not allow me to address them directly…<xsl:copy-of select="$attributes_body/@*/> returns an error

Here is the stylesheet fragment (reduced from original)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="list">
  <xsl:for-each select="figure">
  <xsl:variable name="attributes_body">
     <xsl:attribute name="chapter"><xsl:value-of select="@chapter"/></xsl:attribute>
     <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
   </xsl:variable>
   <xsl:variable name="attributes_other">
      <xsl:attribute name="chapter"><xsl:value-of select="@book"/></xsl:attribute>
      <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
   </xsl:variable>
    <xsl:choose>
       <xsl:when test="ancestor::body">
          <xsl:element name="entry">
            <xsl:copy-of select="$attributes_body"/>
            <xsl:text>Body fig</xsl:text>
          </xsl:element>
       </xsl:when>
       <xsl:otherwise>
          <xsl:element name="entry">
            <xsl:copy-of select="$attributes_other"/>
            <xsl:text>other fig</xsl:text>
          </xsl:element>
       </xsl:otherwise>
    </xsl:choose>         
  </xsl:for-each>    
</xsl:template>

If this can not be done in XLST 1.0 would 2.0 be able to do this?

  • 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-31T16:17:44+00:00Added an answer on May 31, 2026 at 4:17 pm
    <xsl:variable name="attributes_body"> 
         <xsl:attribute name="chapter"><xsl:value-of select="@chapter"/></xsl:attribute> 
         <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute> 
     </xsl:variable>
    

    You need to select the wanted attributes — not to copy their contents in the body of the variable.

    Remember: Whenever possible, try always to specify an XPath expression in the select attribute of xsl:variable — avoid copying content in its body.

    Solution:

    Just use:

    <xsl:variable name="attributes_body" select="@chapter | @id"> 
    

    Here is a complete example:

    <xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
         <xsl:template match="x/a">
          <xsl:variable name="vAttribs" select="@m | @n"/>
    
          <newEntry>
            <xsl:copy-of select="$vAttribs"/>
            <xsl:value-of select="."/>
          </newEntry>
         </xsl:template>
    </xsl:stylesheet>
    

    when applied on this:

    <x>
     <a m="1" n="2" p="3">zzz</a>
    </x>
    

    produces:

     <newEntry m="1" n="2">zzz</newEntry>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have big number, time (micro seconds) stored in two 32bit variables. I need
I have big string in that I need to check if number is present
Im programming C# WinForm application which have big SQL Server Database. I need to
I have big URL list, which I have to download in parallel and check
I have big set of urls and I want to implement an autocompletion. I
i have big decision tree with many Yes/No questions. i need implement some code
I have big sql file, i need to change some lines there, now i
I have big problems with MediaStore. I need to handle events when MediaStore is
I have big problems with stopping a service, which plays audio file. I want
Hi I have big xml file which has a structure similar to the one

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.