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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:50:03+00:00 2026-06-18T11:50:03+00:00

Hey I need to trim content within in each <a> tag to a character

  • 0

Hey I need to trim content within in each <a> tag to a character limit 20 within a description node using XML 1.0. Here is the XML

  <description> 
       This is text <a href="http://stackoverflow.com/posts/14718323/edit">http://stackoverflow.com/posts/14718323/edit</a>. 
       Also here is more text than we have another 
       <a href="http://stackoverflow.com/posts/14718323/edit">http://stackoverflow.com/posts/14718323/edit</a>.
  </description>

What I need it to turn into is this:

  <description> 
       This is text <a href="http://stackoverflow.com/posts/14718323/edit">http://stacko</a>. 
       Also here is more text than we have another 
       <a href="http://stackoverflow.com/posts/14718323/edit">http://stacko</a>.
  </description>

I can do most the logic, but I’m having trouble doing a “for-each” that searches through the description node and transforms “each” < a >.

Does this make sense? Any help appreciated.

** EDIT 2/7/13 *

Based on the answers provided here is where I am at now.

   <xsl:template match="/">
     <xsl:apply-templates select="//description"/>
   </xsl:template>

   <xsl:template match="a">
    <a href="{@href}">
       <xsl:value-of select="substring(normalize-space(),1,20)"/>
    </a>
   </xsl:template>

The problem is “apply-templates” won’t work because I have multiple templates in my XSL. I need to call a template specifically, so I assumed “call-template” would be the route to go. The only problem with “call-template” is I don’t know how to specify an specific XML node to reference. Here is how I’ve hacked it so far (deosn’t work):

    <xsl:template match="/">
      <xsl:call-template name="trim_text"/>
     </xsl:template>

    <xsl:template name="trim_text" match="//description">
     <a href="{@href}">
       <xsl:value-of select="substring(normalize-space(),1,20)"/>
     </a>
    </xsl:template>

The initial ‘call-template’ needs to be in a <xsl:template match="/"> because this is going in a much larger function. So I need 3 things:

1) the HREF to stay consistent with what is in the XML

2) The text between the <a> tag to be trimmed to 20px

3) I need to call this template from inside a much larger xsl template which does a lot of transformation on the XML. This will be of around 7 template calls.

  • 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-18T11:50:04+00:00Added an answer on June 18, 2026 at 11:50 am

    The problem is “apply-templates” won’t work because I have multiple templates in my XSL. I need to call a template specifically, so I assumed “call-template” would be the route to go. The only problem with “call-template” is I don’t know how to specify an specific XML node to reference.

    Template modes may be what you are looking for

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
      <xsl:template match="/">
        <!-- do some other stuff ... -->
    
        <!-- handle the description -->
        <xsl:apply-templates select=".//description" mode="description" />
    
        <!-- more other stuff here -->
      </xsl:template>
    
      <!-- description-specific templates -->
      <xsl:template match="@* | node()" mode="description">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()" mode="description" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="a" mode="description">
        <xsl:copy>
          <xsl:apply-templates select="@*" mode="description" />
          <xsl:value-of select="substring(., 1, 20)" />
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    When you specify a mode with apply-templates, only templates that are marked with the same mode are considered when finding the template to apply to each node (plus the default implicit template that matches all element nodes and recursively applies templates to children using the same mode, but that doesn’t apply here as we have an identity template). This example template for a elements doesn’t allow for links that contain other tags, for example

    <a href="#">This is a <b>very</b> long piece of text used as an example</a>
    

    would become

    <a href="#">This is a very long </a>
    

    If you want to preserve such markup it gets much more complicated. You can’t do it with the obvious approach of

    <xsl:template match="a//text()" mode="description">
      <xsl:value-of select="substring(., 1, 20)"/>
    </xsl:template>
    

    because this would treat each text node in isolation, and produce

    <a href="#">This is a <b>very</b> long piece of text </a>
    

    (as “This is a ” and “very” are already shorter than 20 characters, and ” long piece of text ” is the truncation to 20 characters of ” long piece of text used as…”)

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

Sidebar

Related Questions

Hey, I'm using php 5 and need to communicate with another server that runs
Hey, I'm really stuck with my project here... I need to know when any
Hey Guys...need help. Working on a project and get this error on the Output
I need to pre-compress some very large html/xml/json files (large data dumps) using either
Hey guys I need to send the content of an NSMutableArray through email. I
Hey I'm in need of some help I have made this gallery plugin where
Hey. I need to do something like this: <td><g:formatDate format=yyyy-MM-dd date=${it.conference.startDate}/> (S) <u><g:formatDate format=yyyy-MM-dd
Hey i was wondering do you need root to do this any more? I
Hey, need a little help here. I have two models: class User < ActiveRecord::Base
Hey all need a lil help sorting a loop for this table out, cant

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.