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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:52:17+00:00 2026-05-16T07:52:17+00:00

How can I apply XSLT on following XML so string between ~ and $

  • 0

How can I apply XSLT on following XML so string between ~ and $ becomes Red in output.

The following XSLT work when you have only one string which contains ~ and $. it will not work when you have more than one string which contains ~ and $. info
I am using the same template for DATAC ‘

I am using Java to compare Strings.

I have a option to change code in java or in XSLT.

Thanks

XML

   <?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='StyleSheet.xsl'?>
<log >
       <rows>
        <ID>1</ID>
        <DataP>
  BookID = UJ2445320A
  Qty =  1 ISBN = 45320A 
  ~publishDate = 1/1/2006 $
  ~Name =Learn XML $
  </DataP>
  <DataC>
  BookID = UJ2445320A
  Qty =  1 ISBN = 45320A 
  ~publishDate =2/2/2010$
  ~Name =Learn XML 1.0 $
  </DataC>
             </rows>
  </log>

XSLT

<xsl:for-each select="rows">
    <tr>
   <td><xsl:value-of select="ID"/></td>
   <xsl:apply-templates select="DataP"/>
   <xsl:apply-templates select="DataC"/>
   </tr>
  </xsl:for-each>
 </xsl:for-each>
</xsl:template>
   <xsl:template  match="DataP">  
  <xsl:choose>
   <xsl:when test="contains(.,'~')">
    <td>
     <xsl:value-of select="substring-before(.,'~')"/> 
     <span style="color:red;"><xsl:value-of select="substring-before(substring-after(.,'~'),'$')"/></span>
     <xsl:value-of select="substring-after(.,'$')"/>
    </td>
   </xsl:when>  
   <xsl:otherwise>   
    <td><xsl:value-of select="."/></td> 
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

JAVA CODE

StringBuilder sbcp = new StringBuilder();
StringBuilder sbpp = new StringBuilder();
String[] spilt = StringUtils.split(DataC, "|");
String[] spilt2 = StringUtils.split(DataP, "|");

for (int i = 0; i < spilt.length; i++)
        {
            if(spilt2[i].toString().equals(spilt[i]))
            {
                sbcp.append(spilt[i]);
                sbpp.append(spilt2[i]);
            }
            else
            {
                sbcp.append("~").append(spilt[i]).append("$");
                sbpp.append("~").append(spilt2[i]).append("$");
            }
        }
  • 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-16T07:52:17+00:00Added an answer on May 16, 2026 at 7:52 am

    Here is a complete XSLT 1.0 solution:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/*">
      <table border="1">
       <xsl:apply-templates/>
        </table>
     </xsl:template>
    
     <xsl:template match="rows">
        <tr>
       <xsl:apply-templates/>
        </tr>
     </xsl:template>
    
     <xsl:template match="ID">
       <td><xsl:value-of select="."/></td>
     </xsl:template>
    
     <xsl:template  match="DataP|DataC">
      <td>
       <xsl:call-template name="formatText">
         <xsl:with-param name="pText" select="."/>
       </xsl:call-template>
      </td>
     </xsl:template>
    
     <xsl:template name="formatText">
      <xsl:param name="pText"/>
      <xsl:param name="pStartDelim" select="'~'"/>
      <xsl:param name="pEndDelim" select="'$'"/>
    
      <xsl:if test="string-length($pText)">
          <xsl:variable name="vText" select=
               "concat($pText, $pStartDelim)"/>
          <xsl:variable name="vBeforePat" select=
               "substring-before($vText, $pStartDelim)"/>
          <xsl:variable name="vInText" select=
           "substring-before(substring-after($vText, $pStartDelim),
                             $pEndDelim
                            )
          "/>
    
            <xsl:variable name="vExistsInText"
                 select="string-length($vInText)"/>
    
         <xsl:value-of select="$vBeforePat"/>
    
         <xsl:if test="$vExistsInText">
          <span style="color:red;">
            <xsl:value-of select="$vInText"/>
          </span>
    
          <xsl:call-template name="formatText">
           <xsl:with-param name="pText" select=
            "substring($pText,
                       1
                       + string-length($vBeforePat)+1
                       + $vExistsInText
                       + boolean($vExistsInText) 
                       )
            "/>
          </xsl:call-template>
         </xsl:if>
      </xsl:if>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on this XML file:

    <log>
        <rows>
            <ID>1</ID>
            <DataP>
      BookID = UJ2445320A
      Qty =  1 ISBN = 45320A
      ~publishDate = 1/1/2006 $;
      ~Name =Learn XML $
            </DataP>
            <DataC>
      BookID = UJ2445320A
      Qty =  1 ISBN = 45320A
      ~publishDate =2/2/2010$;
      ~Name =Learn XML 1.0 $
            </DataC>
        </rows>
    </log>
    

    the wanted, correct output is produced:

    <table border="1">
       <tr>
          <td>1</td>
          <td>
      BookID = UJ2445320A
      Qty =  1 ISBN = 45320A
      <span style="color:red;">publishDate = 1/1/2006 </span>;
      <span style="color:red;">Name =Learn XML </span>
    
          </td>
          <td>
      BookID = UJ2445320A
      Qty =  1 ISBN = 45320A
      <span style="color:red;">publishDate =2/2/2010</span>;
      <span style="color:red;">Name =Learn XML 1.0 </span>
    
          </td>
       </tr>
    </table>
    

    Do note how the formatting of the text is achieved via a recursive named template.

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

Sidebar

Related Questions

Is there any command in gdb by which one can apply breakpoint at the
How can I convert the following XML to an escaped text using XSLT? Source:
I have a table with a few relational columns and one XML column which
I'm transforming xml using the following code. It works fine for one xslt but
I have an XML document which contains the following example extract: <p> Some text
here i need a batch file which can apply and create label or base
How do I apply a class to the following so that I can apply
I have a an application with interns who can apply to internships. We have
I have a following XML file: <titles> <book title=XML Today author=David Perry/> <book title=XML
I have the following simplified XML source: <?xml version=1.0 encoding=UTF-8?> <MVKE> <item> <MANDT>025</MANDT> <MATNR>000000000000000551</MATNR>

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.