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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:28:02+00:00 2026-05-25T02:28:02+00:00

there is a case, that appears often times. I am parsing an XML and

  • 0

there is a case, that appears often times. I am parsing an XML and generate my XHTML document via XSLT 1.0.

Case:

/* XML */
<Image src="path-to-img.jpg" href="link-to-page.html" />

/* XSL */
<xsl:choose>
    <xsl:when test="@href">
    <a href="{@href}">
       <img src="{@src}"/>
    </a>
</xsl:when>
<xsl:otherwise>
    <img src="{@src}"/>
</xsl:otherwise>
</xsl:choose>

You see the problem: I am just fetching the case if there is a href set. I’m not satisfied with this approach, but I don’t see another option for implementing this.

Any ideas?

  • 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-25T02:28:03+00:00Added an answer on May 25, 2026 at 2:28 am

    The way to eliminate explicit conditional instructions inside a template is to use pattern-matching within the match pattern of a template:

    <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="Image[@href]">
        <a href="{@href}">
         <xsl:call-template name="basicImage" />
        </a>
     </xsl:template>
    
     <xsl:template match="Image" name="basicImage">
       <img src="{@src}"/>
     </xsl:template>
    </xsl:stylesheet>
    

    XSLT 2.0: There is an especially ellegant solution using <xsl:next-match>:

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="Image[@href]">
        <a href="{@href}">
        <xsl:next-match/>
        </a>
     </xsl:template>
    
     <xsl:template match="Image" name="basicImage">
       <img src="{@src}"/>
     </xsl:template>
    </xsl:stylesheet>
    

    Both transformations, when applied on the provided XML document:

    <Image src="path-to-img.jpg" href="link-to-page.html" />
    

    produce the wanted, correct result:

    <a href="link-to-page.html">
       <img src="path-to-img.jpg"/>
    </a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ignoring the IE case, are there any other browsers that can't understand the application/xhtml+xml
I have the following use case: There's a class called Template and with that
Are there rules of thumb that help determine which to use in what case?
Is there a reason I am missing that a block within a case statement
It's a simple case of a javascript that continuously asks are there yet? Like
Is there a way to enable exclusive checkouts on clear case? I want that
edit It appears that this is just a case of the sample code being
Is their any cases in C++ Like these case WM_COMMAND: switch(LOWORD(wParam)) That happen when
The case: There is a .net application calling unmanaged C code. Used method for
Is there a case insensitive version of the :contains jQuery selector or should 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.