Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old April 4th, 2006, 03:46 PM
Authorized User
 
Join Date: Mar 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Text Replace with XSLT

Hi,

   I have a xml like

      <p>Tested by: pendyala, testuser</p>
      <p>Edited: pendyala,</p>

 I need to parse this like the following way:

  1. Tested by: should be bold and pendyala, testuser should be italic in the same paragraph+
  2. Edited should be bold and seperate paragraph and remaining text as it is and different paragraph.

Can soembody give the idea, how can i achive this one(text node parsing)?

Thanks,
Pendyalap




 
Old April 4th, 2006, 04:23 PM
Authorized User
 
Join Date: Mar 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i did like this:
<xsl:variable name="ReportedBy">Tested by:</xsl:variable>
<xsl:variable name="EditorialNote">Edited:</xsl:variable>


<xsl:template match="text()">
                        <xsl:choose>
                    <xsl:when test="contains(.,$ReportedBy)">
                             <b><xsl:value-of select="$ReportedBy"/></b><i><xsl:value-of select="substring-after(.,$ReportedBy)"/></i>
                      </xsl:when>
                          <xsl:when test="contains(.,$EditorialNote)">
                             <b><h3><xsl:value-of select="$EditorialNote"/></h3></b><xsl:text disable-output-escaping="yes">&lt;/p&gt;&lt;p&gt;</xsl:text><xsl:value-of select="substring-after(.,$EditorialNote)"/>
                      </xsl:when>
                    <xsl:otherwise>
                                <xsl:copy-of select="."/>
                    </xsl:otherwise>
              </xsl:choose>

</xsl:template>
it works for me...if there is any better approach other than this please let me know...

 
Old April 4th, 2006, 05:14 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT 2.0 is a lot more powerful than 1.0 for analyzing the textual content but this one is quite within the capabilities of 1.0. I would do it like this:

<xsl:template match="p[starts-with(., 'Tested by:')]">
<p>
  <span class="bold">Tested by:</span>
  <span class="italic">
    <xsl:value-of select="substring-after(., ':')"/>
  </span>
</p>
</xsl:template>

<xsl:template match="p[starts-with(., 'Edited:')]">
<p class="bold">Edited:</p>
<p>
    <xsl:value-of select="substring-after(., ':')"/>
</p>
</xsl:template>

Using appropriate CSS definitions to achieve the detailed rendition.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 5th, 2006, 09:49 AM
Authorized User
 
Join Date: Mar 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much. this one is much better than me. I will use this.

Quote:
quote:Originally posted by mhkay
 XSLT 2.0 is a lot more powerful than 1.0 for analyzing the textual content but this one is quite within the capabilities of 1.0. I would do it like this:

<xsl:template match="p[starts-with(., 'Tested by:')]">
<p>
<span class="bold">Tested by:</span>
<span class="italic">
    <xsl:value-of select="substring-after(., ':')"/>
</span>
</p>
</xsl:template>

<xsl:template match="p[starts-with(., 'Edited:')]">
<p class="bold">Edited:</p>
<p>
    <xsl:value-of select="substring-after(., ':')"/>
</p>
</xsl:template>

Using appropriate CSS definitions to achieve the detailed rendition.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 26th, 2006, 01:55 PM
Registered User
 
Join Date: Sep 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jlowery
Default

 
Quote:
quote:<xsl:template match="p[starts-with(., 'Tested by:')]">
Quote:
<p>
<span class="bold">Tested by:</span>
<span class="italic">
<xsl:value-of select="substring-after(., ':')"/>
</span>
</p>
</xsl:template>
Tried this on a script of mine, but found I needed to specify the text() node:

<xsl:template match="x:html/x:body/text()[starts-with(normalize-space(),'javax.tv')]">
&cr;Class: <xsl:value-of select="normalize-space()" />&cr;
    </xsl:template>


This is using Saxonica 8, xslt 2.0. Change in behavior of XSLT, bug in Saxonica, or bug in script writer?

--Jeff

 
Old September 26th, 2006, 02:03 PM
Registered User
 
Join Date: Sep 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jlowery
Default

I tried doing the dot thing, but that didn't work, either:

   <xsl:template match="x:html/x:body[starts-with(normalize-space(.),'javax.tv')]">
&cr;Class: <xsl:value-of select="normalize-space()" />&cr;
    </xsl:template>



 
Old September 26th, 2006, 02:33 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The pattern

match="x:html/x:body/text()"

seems very odd because XHTML doesn't allow the body element to have text node children. Sometimes you have to match text nodes when handling mixed content, but it's unusual. If you tried something and it didn't work, then you need to tell us exactly what you tried and exactly how it didn't work.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 26th, 2006, 02:50 PM
Registered User
 
Join Date: Sep 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jlowery
Default

Its a very long, flat, and ugly generated xhtml document that I'm parsing. And, yes, it does have mixed content.

I'd be happy to forward a zipped up copy to you offline (along with my script) and we could continue the discussion from there. The script is a simple one and only takes a minute to run. The variations I've tried are pretty much as I've described; if text() is not specified in the match, I get no stinking matches.

I'm sure I've hosed it somehow...

 
Old September 26th, 2006, 04:20 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try to construct a compact example that demonstrates the problem, with enough information to enable a reader to run it for themselves, and post it on this list. The exercise of doing this may well give you insights into the problem.

I'm not prepared to continue discussions off-list (except with paying Saxonica customers). That creates an expectation that I will pursue the discussion to a conclusion.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 26th, 2006, 05:03 PM
Registered User
 
Join Date: Sep 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jlowery
Default

Quote:
quote:
I'm not prepared to continue discussions off-list (except with paying Saxonica customers). That creates an expectation that I will pursue the discussion to a conclusion.
I should have said "continue the discussion here", which is what I meant.

document stub (may or may not be actual XHTML, but it's well-formed and that's what I needed):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta name="generator" content=
  "HTML Tidy for Windows (vers 14 February 2006), see www.w3.org" />

  <title>untitled</title>
  <meta http-equiv="Content-Type" content=
  "text/html; charset=us-ascii" />
  <meta name="generator" content="pdftohtml 0.39" />
  <meta name="date" content="2005-04-25T23:35:02+00:00" />
</head>

<body bgcolor="#A0A0A0" vlink="blue" link="blue">
  <a name="398"></a><b>396</b><br />
  <br />
  <b>Annex A (normative):</b><br />
  <b>External references; errata,</b><br />
  <b>clarifications and exemptions</b><br />
  This clause lists known errata in normative external references,
  as well as clarifications to those references and/or<br />
  exemptions from requirements in those specifications.<br />
  A.5.3<br />
  javax.tv.util.TVTimerSpec<br />
  A.5.3.1<br />
  setAbsoluteTime(long)<br />
  The following specification additions are considered to be
  present:<br />
  If the time parameter passed is negative, this method shall throw
  an IllegalArgumentException.<br />
  A.5.3.2<br />
  setTime(long)<br />
  The following specification additions are considered to be
  present:<br />
  If the time parameter passed is negative, this method shall throw
  an IllegalArgumentException.<br />
  A.5.4<br />
  javax.tv.xlet.Xlet<br />
  A.5.4.1<br />
  Xlet state descriptions<br />
  In the table of xlet state descriptions in the package
  description of the javax.tv.xlet package, the description of
  the<br />
  loaded state is considered to be modified as follows.<br />
  <i><b>DVB Bluebook A068 Rev. 1</b></i><br />

  <a name="401"></a><b>399</b><br />
</body>
</html>


script (non-working; to fix swap the commented-out templates with corresponding ones above):

<!DOCTYPE stylesheet [
<!ENTITY cr "<xsl:text>
</xsl:text>">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0" xmlns:x="http://www.w3.org/1999/xhtml">

    <xsl:output indent="yes" encoding="utf-8" method="text" />
    <xsl:variable name="sig">^[a-zA-Z0-9]+\(.*?\)$</xsl:variable>


    <xsl:template match="x:b">
        <xsl:variable name="X">
            <xsl:value-of select="normalize-space()" />
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="starts-with($X, 'org.dvb')">&cr;Class: <xsl:value-of select="$X"/>&cr;</xsl:when>
            <xsl:when test="starts-with($X, 'org.davic')">&cr;Class: <xsl:value-of select="$X"/>&cr;</xsl:when>
             <xsl:when test="matches($X, $sig)">Method: <xsl:value-of select="replace($X,':','')"/>&cr;</xsl:when>
        </xsl:choose>
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="/x:html/x:body[starts-with(normalize-space(),'org.havi')]">
        &cr;Class: <xsl:value-of select="normalize-space()" />&cr;
    </xsl:template>

    <xsl:template match="x:html/x:body[starts-with(normalize-space(),'javax.tv')]">
        &cr;Class: <xsl:value-of select="normalize-space()" />&cr;
    </xsl:template>

<!--
    <xsl:template match="/x:html/x:body/text()[starts-with(normalize-space(),'org.havi')]">
        &cr;Class: <xsl:value-of select="normalize-space()" />&cr;
    </xsl:template>

    <xsl:template match="x:html/x:body/text()[starts-with(normalize-space(),'javax.tv')]">
        &cr;Class: <xsl:value-of select="normalize-space()" />&cr;
    </xsl:template>
-->
    <xsl:template match="/x:html/x:body/text()[matches(normalize-space(), $sig)]">Method: <xsl:value-of

select="replace(normalize-space(),':','')"/>&cr;
    </xsl:template>

    <xsl:template match="text()|@*"></xsl:template>
</xsl:stylesheet>


output (non-working):

Method: setAbsoluteTime(long)
Method: setTime(long)


output (working):

Class: javax.tv.util.TVTimerSpec
Method: setAbsoluteTime(long)
Method: setTime(long)

Class: javax.tv.xlet.Xlet


-- Jeff







Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use replace function() xslt 2.0 dev.user06 XSLT 11 June 21st, 2012 04:00 AM
XSLT replace for character entities atulshin XSLT 1 November 1st, 2008 05:19 AM
xslt replace function rajesh_css XSLT 7 October 30th, 2008 08:39 PM
replace text without using regex mrame XSLT 7 July 28th, 2008 09:12 AM
Replace xml attribs vals with xslt andye XSLT 2 July 30th, 2006 06:48 PM





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