0

Hi I have the below line in by XML and also I need a hyperlink for the number. I want this output to be shown in HTML format.

<main>
<alph>a b 2,3</alph>
</main>

I want an XSLT that gives output as:

a b 2, a b 3

I have tried the below XSLT:

<xsl:template match="alph">
  <xsl:variable name="link" select="normalize-space(translate(
    normalize-space(current()),abcdefghijklmnopqrstuvwxyz,''))"/>
  <xsl:value-of select="substring-before(normalize-space(.),$link)"/>
  <xsl:variable name="tex">
    <xsl:value-of select="text()"/>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="contains($link,',')">
      <xsl:variable name="new">
        <xsl:value-of select="tokenize($link,',')"/>
      </xsl:variable>
      <xsl:value-of select="concat($new,$tex)"/>
    </xsl:when>
    <xsl:when test="contains($link,'-')">
      <xsl:value-of select="tokenize($link,'-')"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$link"/>
    </xsl:otherwise>
  </xsl:choose>

But it is giving me output as:

a b 2 3a b 2,3

Thanks

5
  • I am guessing that you are using XSLT 2.0?
    – Pablo Pozo
    Feb 25, 2013 at 15:13
  • yes friend i'm using xslt 2.0. could you please edit or provide me a solution on how to do this. please Feb 25, 2013 at 15:15
  • can you please let me know how to do it in XSLT1.0 thanks Feb 25, 2013 at 15:31
  • The expression that you wrote is always going to be like that, or you can have more commas, e.g. a b 1,4,5,6 so the output should be a b 1, ab 4, a b 5, a b 6. Also how many elements can be present before the comma separated list? Could you define precisely the structure of the expression in a general way?
    – Pablo Pozo
    Feb 25, 2013 at 15:51
  • there would be only 1 element before comma Feb 25, 2013 at 18:40

1 Answer 1

1

One problem you have is with the variable link

<xsl:variable name="link" select="normalize-space(translate(
     normalize-space(current()),abcdefghijklmnopqrstuvwxyz,''))"/>

It looks like you are trying removing all alphabetic characters from the string, so that you are left with just 2,3. However, for this to work the abc...xyz needs to be enclosed in apostrophes, otherwise it will be looking for an element named abc...xyz. Having said that, you say you are using XSLT2.0, so you can make use of the replace function here, which takes a regular expression as a parameter

<xsl:variable name="link" select="normalize-space(replace(current(),'[a-z]',''))"/>

Next, you can get the text before this link, like so

<xsl:variable name="text" select="normalize-space(substring-before(current(), $link))"/>

This will give you your a b

Finally, you can use the tokenize function to split up the 2,3. In your XSLT you seem to be looking for hyphens too, but the tokenize function also uses regular expressions, so this is not a problem. What you can do is just tokenize the string, and re-join it using the text variable as a separator

  <xsl:value-of select="concat($text, ' ')"/>
  <xsl:value-of select="tokenize($link,',|-')" separator="{concat(', ', $text, ' ')}"/>

Here is the full XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text"/>
   <xsl:template match="alph">
      <xsl:variable name="link" select="normalize-space(replace(current(),'[a-z]',''))"/>
      <xsl:variable name="text" select="normalize-space(substring-before(current(), $link))"/>
      <xsl:value-of select="concat($text, ' ')"/>
      <xsl:value-of select="tokenize($link,',|-')" separator="{concat(', ', $text, ' ')}"/>
   </xsl:template>
</xsl:stylesheet>

When applied on your XML, the following is output

a b 2, a b 3
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.