tags:

views:

90

answers:

3

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("$");
            }
        }
A: 

I think regex could be a way to go. You just have to build '~ someString $' pattern and turn them into red color.

sarahTheButterFly
A: 

you could try the xsl analyze string:

<xsl:template  match="DataP">
  <xsl:analyze-string select="." regex="~(.*?)\$">
    <xsl:matching-substring>
       <span style="color:red;"><xsl:value-of select="regex-group(1)"/></span>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
      <xsl:value-of select="."/>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template> 

Not sure if the regex is correct, you should doublecheck it.

davyM
perhaps ~([^$]*)\$ would be better?
Maurice Perry
+2  A: 

Here is a complete XSLT 1.0 solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <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.

Dimitre Novatchev