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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:47:27+00:00 2026-06-06T20:47:27+00:00

How can we create attributes based on some condition in xslt. My input xml

  • 0

How can we create attributes based on some condition in xslt.

My input xml has a tag:

          <track external="http://mysite.com"  />
              or
           <track  local="/myfolder" />

and in this ‘track element either external or local attribute appears but not either of these and i have to convert it into

       <a xhtml:href="@external value" xmlns="http://www.w3.org/1999/xhtml" /> 

if ‘extrenal’ attribute occurs for ‘track’ element or into

       <a xlink:href="@local value" xmlns="http://www.w3.org/1999/xlink" /> 

if ‘local’attribute occurs for ‘track’ element

XSLT tried:

     <a xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink">      

      <xsl:for-each select="child::*" >
        <xsl:choose>

          <xsl:when test="name()='track'">
             <xsl:if test="@local">
            <xsl:attribute name="xlink:href">

            <xsl:value-of select="@local" />
            </xsl:attribute>
            </xsl:if>
               <xsl:if test="@external">
            <xsl:attribute name="xhtml:href">

            <xsl:value-of select="@external" />
            </xsl:attribute>
            </xsl:if>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>

      </a>

but exception is thrown as i created attributes for ‘a’ element based on a condition. This is not accepted in XSLT 1.0, is there any way to make attribute appear for my ‘a’ element based on some condition in XSLT 1.0.

  • 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-06T20:47:30+00:00Added an answer on June 6, 2026 at 8:47 pm

    There is nothing preventing you conditionally adding attributes in XSLT 1.0. Incidentally, avoid for-each in XSLT and use templates instead. Here’s a dedicated template for track nodes.

    <xsl:template match='track'>
    
        <a xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink">
            <xsl:if test="@local">
                <xsl:attribute name="xlink:href">
                        <xsl:value-of select="@local" />
                </xsl:attribute>
            </xsl:if>
            <xsl:if test="@external">
                <xsl:attribute name="xhtml:href">
                    <xsl:value-of select="@external" />
                </xsl:attribute>
            </xsl:if>
        </a>
    
    </xsl:template>
    

    As Tim C mentioned, though, a better pattern would be to handle the different types of incoming attribute via different templates. As much as anything, this is cleaner, as you don’t get bogged down in conditional blocks.

    <xsl:template match='track'>
        <a><xsl:apply-templates select='@*' /></a>
    </xsl:template>
    
    <xsl:template match='@local' xmlns:xlink="http://www.w3.org/1999/xlink">
        <xsl:attribute name="xlink:href">
                <xsl:value-of select="." />
        </xsl:attribute>
    </xsl:template>
    
    <xsl:template match='@external' xmlns:xhtml="http://www.w3.org/1999/xhtml">
        <xsl:attribute name="xhtml:href">
            <xsl:value-of select="." />
        </xsl:attribute>
    </xsl:template>
    

    You can see both approaches in action in this XMLPlayground.

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

Sidebar

Related Questions

I want to create properties in class. Where can I create them in attributes
Can Permalink_fu combine 2 or more model attributes to create a unique permalink? Let's
I was wondering, when you create an object you can often set multiple attributes
Here's the deal: I have a report designer where users can create reports based
I have some xml that is provided with generic attributes (name, type, etc), where
I can't access my model's attributes in the after_create callback... seems like I should
Can I create an attribute that will let me modify the value of it
How can i create an attribute with a namespace? To get the following output?
I can create and use dynamic two dimensional array in Fortran (in 77 standard).
You can create a table model and add it to a table TableModel tm

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.