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 December 22nd, 2004, 09:21 AM
Registered User
 
Join Date: Dec 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Certainly:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2004/07/xpath-functions" xmlns:xdt="http://www.w3.org/2004/07/xpath-datatypes">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="filters">
  <filter text="target" replace="objective"/>
  <filter text="anywhere" replace="somewhere"/>
  <filter text="hi" replace="hello"/>
  <filter text="secret" replace=" "/>
</xsl:variable>

<xsl:template name="filter">
<xsl:param name="str"/>
<xsl:choose>
  <xsl:when test="$filters/filter[contains($str, @text)]">
    <xsl:variable name="match" select="$filters/filter[contains($str, @text)]"/>
    <xsl:variable name="word" select="$match/@text"/>
    <xsl:variable name="repl" select="$match/@replace"/>
      <xsl:call-template name="filter">
      <xsl:with-param name="str">
        <xsl:value-of select="substring-before($str,$word)"/>
        <xsl:value-of select="$repl"/>
        <xsl:value-of select="substring-after($str,$word)"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:copy-of select="$str"/>
  </xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="/">
<xsl:call-template name="filter">
  <xsl:with-param name="str" select="self::text()"/>
</xsl:call-template>
<xsl:apply-templates/>
</xsl:template>

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

</xsl:stylesheet>
 
Old December 22nd, 2004, 03:11 PM
Registered User
 
Join Date: Dec 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I looked at the template 'filter' and everything made sense to me as to what it was matching against and replacing. The only thing that I could think of was that it was actually looking at the values of the attributes. Could the problem be with having multiple elements within a global variable, and/or trying to match against their attributes? I added some text in the $filters/filter elements to see some things. For example:

<xsl:variable name="filters">
  <filter1 text="target" replace="objective">word1</filter1>
  <filter text="anywhere" replace="somewhere">word2</filter>
  <filter text="hi" replace="hello">word3</filter>
  <filter text="secret" replace=" ">word4</filter>
</xsl:variable>

<xsl:variable name="longfilters">
    <filter1>
        <text>target</text>
        <replace>objective</replace>
    </filter1>
    <filter>
        <text>anywhere</text>
        <replace>somewhere</replace>
    </filter>
    <filter>
        <text>hi</text>
        <replace>hello</replace>
    </filter>
    <filter>
        <text>secret</text>
        <replace>Secret</replace>
    </filter>
</xsl:variable>

<xsl:template name="filter">
************output******************
(1)<xsl:value-of select="$filters"/>
(2)<xsl:value-of select="$filters/filter1"/>
(3)<xsl:value-of select="$filters/filter/@text"/>
(4)<xsl:value-of select="$filters/filter,@text"/>
(5)<xsl:value-of select="$longfilters"/>
(6)<xsl:value-of select="$longfilters/filter"/>
(7)<xsl:value-of select="$longfilters/filter/text"/>
(8)<xsl:value-of select="$longfilters/filter1/text"/>
***********end output***************
</xsl:template>

<xsl:template match="/">
<xsl:call-template name="filter"/>
<xsl:apply-templates/>
</xsl:template>

and the output that I get is:

************output******************
(1)word1 word2 word3 word4
(2)
(3)
(4)
(5)targetobjective anywheresomewhere hihello secretSecret
(6)
(7)
(8)
***********end output***************
 
Old December 22nd, 2004, 03:14 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

downloaded a trial copy of xmlspy2005 yesterday.
XMLSpy 2005 seems to be inconsistent with XMLSpy 2004... I've a working copy for Saxon 8, but it seems to be incompatible with XMLSpy 2005.
working on it now,..
--
Saxon 8 compatible version:
Changes:Only replaces text nodes.
filter variable changed back, because of difference in interpretation of xslt spec.
Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2004/07/xpath-functions" xmlns:xdt="http://www.w3.org/2004/07/xpath-datatypes">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="filters">
<filters>
  <filter text="target" replace="objective"/>
  <filter text="anywhere" replace="somewhere"/>
  <filter text="hi" replace="hello"/>
  <filter text="secret" replace=" "/>
</filters>  
</xsl:variable>

<xsl:template name="filter">
<xsl:param name="str"/>
<xsl:variable name="match" select="$filters/filters/filter[contains($str, @text)][1]"/>
<xsl:choose>
  <xsl:when test="$match">
    <xsl:variable name="word" select="$match/@text"/>
    <xsl:variable name="repl" select="$match/@replace"/> 
      <xsl:call-template name="filter">
      <xsl:with-param name="str">
        <xsl:value-of select="substring-before($str,$word)"/>
        <xsl:value-of select="$repl"/>
        <xsl:value-of select="substring-after($str,$word)"/>   
      </xsl:with-param>
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:copy-of select="$str"/>
  </xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()">
    <xsl:call-template name="filter">
      <xsl:with-param name="str" select="self::text()"/>
    </xsl:call-template>
</xsl:template>


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

</xsl:stylesheet>
MSXML version:
change the match variable to:
<xsl:variable name="match" select="msxml:node-set($filters)/filters/filter[contains($str, @text)]"/>
add the xmlns namespace to the stylesheet:
xmlns:msxml="urn:schemas-microsoft-com:xslt"
and change the version back to 1.0

I tend to think there's a bug in Altova's XPath 2.0 processor.
 
Old December 23rd, 2004, 09:12 AM
Registered User
 
Join Date: Dec 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow. That works very well! Thank you so much for all of your help,... much appreciated!

~Ryan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating, Writing to and Deleting Text Files goels Access VBA 7 January 30th, 2007 11:51 AM
Tags as text in XML elements janise XML 16 January 2nd, 2007 06:45 AM
Changing between bold and plain text in a text box funkybuddha Access 2 January 3rd, 2006 10:15 AM
Deleting text from a RichTextBox wslyhbb C# 0 October 29th, 2005 08:00 PM
Deleting text strings automatically from Database palvin SQL Server 2000 15 October 3rd, 2005 12:52 PM





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