Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 4th, 2007, 12:50 PM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default add HTML tags via XSLT

I'm trying to use XSLT to format XML to HTML.
The thing is I need to format certain nodes by adding HTML tags to the node text
For example, I need to replace one or more strings of "B, " with "<sup>B</sup>, ".
and when it is outputed to HTML I need the tags to read as tags and put the superscript around the "B"

<stuff>item3B, item2, item1B, item4</stuff>
need to be in html
item3<sup>B</sup>, item2, item1<sup>B</sup>, item4

How to do so would be great!
thanks

  #2 (permalink)  
Old November 4th, 2007, 01:06 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0:

<xsl:analyze-string select="stuff" regex="B">
  <xsl:matching-substring>
    <sup><xsl:value-of select="."/></sup>
  </xsl:matching-substring>
  <xsl:non-matching-substring>
    <xsl:value-of select="."/>
  </xsl:non-matching-substring>
</xsl:analyze-string>

In 1.0 it's considerably harder; you need to use recursive templates to process the string using substring-before, contains, and substring-after. There are examples in my book.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #3 (permalink)  
Old November 5th, 2007, 09:24 AM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried that but I got an "Error during XSLT transformation: XSLT transformation failed." error

I'm opening the xml file on FireFox 2.0.0.9
Ideas?

my test files
XML
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="XSL_Test.xsl"?>

<NewDataSet>
  <statblock>
     <stuff>item3B, item2, item1B, item4</stuff>
  </statblock>
</NewDataSet>

XSL
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0" >

   <xsl:output method="html" />



   <xsl:template match="/">
      <html>
         <body>
            <h5>
              <xsl:analyze-string select="stuff" regex="B">
                       <xsl:matching-substring>
                                     <sup><xsl:value-of select="."/></sup>
                              </xsl:matching-substring>
                              <xsl:non-matching-substring>
                               <xsl:value-of select="."/>
                               </xsl:non-matching-substring>
                           </xsl:analyze-string>
    </h5>
 </body>
      </html>
   </xsl:template>

</xsl:stylesheet>

  #4 (permalink)  
Old November 5th, 2007, 09:30 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

That's because Firefox doesn't support XSLT 2.0, only 1.0.

/- Sam Judson : Wrox Technical Editor -/


Similar Threads
Thread Thread Starter Forum Replies Last Post
strip html tags with xslt smiter XSLT 4 June 19th, 2012 08:34 AM
XSLT help : Interpretting HTML tags from XML kunal_kishan XSLT 1 June 26th, 2006 05:37 AM
Can I add code to title tags? wehaveasolution ASP.NET 1.0 and 1.1 Basics 0 October 10th, 2005 09:59 AM
Add XSLT transformed XML (html) on an aspx page humour XSLT 2 September 30th, 2004 05:13 AM
Dynamically Add input tags Jstmehr4u3 Javascript How-To 2 July 29th, 2003 04:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.