1

I want to select part of a String using xslt.

Input :

p:endnote_bl1
p:endnote_bl2
p:endnote_bl3
p:endnote_bl4

Output Should be :

endnote_bl1
endnote_bl2
endnote_bl3
endnote_bl4

Can I do this using split or something else? I am using XSLT 2.0

0

3 Answers 3

1

Use replace function

replace('p:endnote_bl1', '^[^:]*:', '')
Sign up to request clarification or add additional context in comments.

Comments

1

You could do simply:

substring-after($string, ':')

Comments

1

There are a multitude of ways to do this.

This transformation shows four different solutions, the first two of which are XPath 1.0 (XSLT 1.0) solutions:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vSolution1">
   <xsl:apply-templates select="/*" mode="substring-after"/>
 </xsl:variable>

 <xsl:variable name="vSolution2">
   <xsl:apply-templates select="/*" mode="substring"/>
 </xsl:variable>

 <xsl:variable name="vSolution3">
   <xsl:apply-templates select="/*" mode="split"/>
 </xsl:variable>

 <xsl:variable name="vSolution4">
   <xsl:apply-templates select="/*" mode="replace"/>
 </xsl:variable>

  <xsl:template match="/">
    <xsl:copy-of select="$vSolution1"/>
    ==================
    <xsl:copy-of select="$vSolution2"/>
    ==================
    <xsl:copy-of select="$vSolution3"/>
    ==================
    <xsl:copy-of select="$vSolution4"/>

  </xsl:template>

  <xsl:template match="node()|@*" mode="#all">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" mode="#current"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="s/text()" mode="substring-after">
    <xsl:value-of select="substring-after(., 'p:')"/>
  </xsl:template>

  <xsl:template match="s/text()" mode="substring">
    <xsl:value-of select="substring(., 3)"/>
  </xsl:template>

  <xsl:template match="s/text()" mode="split">
    <xsl:value-of select="tokenize(., ':')[2]"/>
  </xsl:template>

  <xsl:template match="s/text()" mode="replace">
    <xsl:value-of select="replace(., '^.+:', '')"/>
  </xsl:template>
</xsl:stylesheet>

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

<t>
    <s>p:endnote_bl1</s>
    <s>p:endnote_bl2</s>
    <s>p:endnote_bl3</s>
    <s>p:endnote_bl4</s>
</t>

We get four identical, correct results, each produced by one of the four solutions:

<t>
      <s>endnote_bl1</s>
      <s>endnote_bl2</s>
      <s>endnote_bl3</s>
      <s>endnote_bl4</s>
</t>
    ==================
    <t>
      <s>endnote_bl1</s>
      <s>endnote_bl2</s>
      <s>endnote_bl3</s>
      <s>endnote_bl4</s>
</t>
    ==================
    <t>
      <s>endnote_bl1</s>
      <s>endnote_bl2</s>
      <s>endnote_bl3</s>
      <s>endnote_bl4</s>
</t>
    ==================
    <t>
      <s>endnote_bl1</s>
      <s>endnote_bl2</s>
      <s>endnote_bl3</s>
      <s>endnote_bl4</s>
</t>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.