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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:34:37+00:00 2026-06-09T05:34:37+00:00

A global variable indicates, for each of various kinds of elements, what attributes need

  • 0

A global variable indicates, for each of various kinds of elements, what attributes need to be processed.

<xsl:variable name="attributes.rtf">
  <element name="measure">
    <att>type</att>
    <att>quantity</att>
    <att>unit</att>
  </element>
  <element name="milestone">
    <att>n</att>
  </element>
  <element name="lb">
    <att>ed</att>
    <att>type</att>
    <att>subtype</att>
    <att>n</att>
  </element>
</xsl:variable>
<xsl:variable name="attributes" select="exslt:node-set($attributes.rtf)" /> <!-- This is XSLT 1.0 -->

When the context is an element, I need a for-each that will loop through attributes for that kind of element. I don’t see how to do this in one XPath expression. Right now I have these two:

 <xsl:variable name="temp" select="." />                            <!-- For, say, a <measure> element, -->
 <xsl:for-each select="$attributes/element[@name=name($temp)]/att"> <!-- loop through the names "type", "quantity", and "unit" -->
      <xsl:for-each select="$temp/@*[name()=current()]">            <!-- If an attribute of that name exists in the current <measure> element  -->
                                                                    <!-- (now stored as $temp), then process that attribute. --> 
      </xsl:for-each>
 </xsl:for-each>

Is there a way to do this in one expression, without creating $temp.

But also, I need an outer test condition that indicates whether the context element has any of the listed attributes at all. For that test, I’d like one expression.

(Processing of the attributes does need to be ordered, so maybe that requires the two loops. But order would not be relevant in the test condition. So maybe just that could be done with one expression.)

  • 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-09T05:34:40+00:00Added an answer on June 9, 2026 at 5:34 am

    I. The “simple” problem:

    When the context is an element, I need a for-each that will loop through attributes for that kind of element.
    

    No xsl-for-each is needed.

    I don't see how to do this in one XPath expression.
    

    Just use:

    <xsl:apply-templates select=
      "@*[name()=$attributes/element[@name=name(current())]/att]"/>
    

    Explanation:

    Proper use of the current() function.


    II. The “complex” problem:

    Processing of the attributes does need to be ordered, so maybe that requires the two loops
    

    Here is a complete example showing that no other nested <xsl:apply-templates> is necessary to produce the attributes in the order specified in $attributes:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:exslt="http://exslt.org/common">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
        <xsl:variable name="attributes.rtf">
            <element name="measure">
                <att>type</att>
                <att>quantity</att>
                <att>unit</att>
            </element>
            <element name="milestone">
                <att>n</att>
            </element>
            <element name="lb">
                <att>ed</att>
                <att>type</att>
                <att>subtype</att>
                <att>n</att>
            </element>
        </xsl:variable>
    
        <xsl:variable name="attributes" select="exslt:node-set($attributes.rtf)" />
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="t/*">
      <xsl:copy>
          <xsl:apply-templates select="$attributes/element[@name=name(current())]/att">
            <xsl:with-param name="pCurrent" select="current()"/>
          </xsl:apply-templates>
          <xsl:apply-templates select=
           "@*[not(name() = $attributes/element[@name=name(current())]/att)]"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="att">
       <xsl:param name="pCurrent" select="/.."/>
    
      <xsl:if test="$pCurrent[@*[name()=current()]]">
          <xsl:attribute name="{.}">
           <xsl:value-of select="concat(.,'-',.)"/>
          </xsl:attribute>
      </xsl:if>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the following XML document (none was provided!!!):

    <t>
      <lb type="normal" ed="uni" n="3"
          subtype="initial" other="yes"/>
      <milestone n="2" other="yes"/>
      <measure quantity="3" unit="cm"
               type="length" other="yes"/>
    </t>
    

    the wanted, correct result is produced:

    <t>
       <lb ed="ed-ed" type="type-type" subtype="subtype-subtype" n="n-n" other="yes"/>
       <milestone n="n-n" other="yes"/>
       <measure type="type-type" quantity="quantity-quantity" unit="unit-unit" other="yes"/>
    </t>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a global JSON variable where I store some parameters and then each
i have a global variable who hold the value for routing i need to
A global variable for a Twig template can be defined inside the config.yml of
Regarding global variable initialization, function hello_testing() { global $conditional_random; if (isset($conditional_random)) { echo foo
I use a global variable (an object of a class) and define it as
I have a global variable averagel that I am trying to manipulate in a
I have a global variable contacts, var contacts = ContactsApp.getContacts(); I have an KeyUphandler
I just create a Global Variable in Ms. Dynamic Nav 2009 R2 and I
If i have global variable in A.dll, that depends on global variable in B.dll
i declare a global variable and use and modify its value in the function.

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.