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 October 28th, 2008, 11:05 PM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default xslt replace function

Hi,

my xml:
<?xml version='1.0'?>
<AAA >
         <BBB ooo = "111" ppp = "222" >
             <CCC>c1 </CCC>
             <CCC>c2 </CCC><break></break>
         </BBB>
         <BBB ooo = "555" ppp = "666" >
             <DDD>d1 </DDD><break></break>
             <DDD>d2 </DDD>
             <DDD>d3 </DDD>
             <DDD>d4 </DDD><break></break>
             <DDD>d5 </DDD>
         </BBB>
     </AAA>

my xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
        <xsl:template match="/">
         <QQQ>
                 <xsl:copy-of select="//BBB" />
         </QQQ>
        </xsl:template>
</xsl:stylesheet>

my html o/p:

<?xml version="1.0" encoding="UTF-8"?><QQQ><BBB ooo="111" ppp="222">
             <CCC>c1 </CCC>
             <CCC>c2 </CCC><break/>
         </BBB><BBB ooo="555" ppp="666">
             <DDD>d1 </DDD><break/>
             <DDD>d2 </DDD>
             <DDD>d3 </DDD>
             <DDD>d4 </DDD><break/>
             <DDD>d5 </DDD>
         </BBB></QQQ>


i want to replace <break></break> with <br></br>
how can i do this.

i tried using this template

<xsl:template name="SubstringReplace">
<xsl:param name="stringIn" />
<xsl:param name="substringIn" />
<xsl:param name="substringOut" />
<xsl:choose>
    <xsl:when test="contains($stringIn,$substringIn)">
    <xsl:value-of
        select="concat(substring-before($stringIn,$substringIn),$substringOut)" />
    <xsl:call-template name="SubstringReplace">
    <xsl:with-param name="stringIn"
    select="substring-after($stringIn,$substringIn)" />
<xsl:with-param name="substringIn"
select="$substringIn" />
<xsl:with-param name="substringOut"
                        select="$substringOut" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$stringIn" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>


and

                            <xsl:copy-of>
                                        <xsl:variable name="myString" select="./node()" />
                                            <xsl:variable name="myNewString">
                                            <xsl:call-template name="SubstringReplace">
                                                <xsl:with-param name="stringIn" select="$myString"/>
                                                <xsl:with-param name="substringIn" select="'break'"/>
                                                <xsl:with-param name="substringOut" select="'br'"/>
                                                </xsl:call-template>
                                                </xsl:variable>
                                            <xsl:value-of select="concat('input: ',$myString,'output: ',$myNewString)"/>
                                                    </xsl:copy-of>



i am not able to compile if i use the above xsl

what may be the problem and how can we declare variables for xsl:copy-of.


Raj


 
Old October 28th, 2008, 11:42 PM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default

hi

i hav tried with this example also

xml:

<?xml version='1.0'?>
<winelist>

  <wine grape="Chardonnay">
    <winery>Benziger</winery>
    <product>Carneros</product>
    <year>1997</year>
    <desc>Well-textured flavors, good finish.</desc>
    <prices>
      <list>10.99</list>
      <discounted>9.50</discounted>
      <case>114.00</case>
    </prices>
  </wine>

  <wine grape="Cabernet">
    <winery>Duckpond</winery>
    <product>Merit Selection</product>
    <year>1996</year>
    <desc>Sturdy and generous flavors, long finish.</desc><finish></finish>
    <prices>
      <list>13.99</list>
      <discounted>11.99</discounted>
      <case>143.50</case>
    </prices>
  </wine>

</winelist>


xsl:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|*">
  <xsl:copy>
    <xsl:apply-templates select="./node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="text()">
  <xsl:call-template name="globalReplace">
  <xsl:with-param name="outputString" select="."/>
  <xsl:with-param name="target" select="'finish'"/>
  <xsl:with-param name="replacement" select="'FINISH'"/>
  </xsl:call-template>
</xsl:template>


    <xsl:template name="globalReplace">
        <xsl:param name="outputString" />
        <xsl:param name="target" />
        <xsl:param name="replacement" />
        <xsl:choose>
            <xsl:when test="contains($outputString,$target)">
                <xsl:value-of select="concat(substring-before($outputString,$target),$replacement)" />
                <xsl:call-template name="globalReplace">
                    <xsl:with-param name="outputString" select="substring-after($outputString,$target)" />
                    <xsl:with-param name="target" select="$target" />
                    <xsl:with-param name="replacement" select="$replacement" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$outputString" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>


my html o/p:

<?xml version="1.0" encoding="UTF-8"?><winelist>

  <wine>
    <winery>Benziger</winery>
    <product>Carneros</product>
    <year>1997</year>
    <desc>Well-textured flavors, good FINISH.</desc>
    <prices>
      <list>10.99</list>
      <discounted>9.50</discounted>
      <case>114.00</case>
    </prices>
  </wine>

  <wine>
    <winery>Duckpond</winery>
    <product>Merit Selection</product>
    <year>1996</year>
    <desc>Sturdy and generous flavors, long FINISH.</desc><finish/>
    <prices>
      <list>13.99</list>
      <discounted>11.99</discounted>
      <case>143.50</case>
    </prices>
  </wine>

</winelist>


but the finish tag is not replaced with FINISH as in the text template we are using select="." ,how can we change the text inside the tag.


......Raj





 
Old October 29th, 2008, 03:19 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I would use a more traditional identity template. Then add the replace function and the template to match text. Then add a new template to match the finish element:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

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

  <xsl:template match="text()">
    <xsl:call-template name="globalReplace">
      <xsl:with-param name="outputString" select="."/>
      <xsl:with-param name="target" select="'finish'"/>
      <xsl:with-param name="replacement" select="'FINISH'"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="globalReplace">
    <xsl:param name="outputString" />
    <xsl:param name="target" />
    <xsl:param name="replacement" />
    <xsl:choose>
      <xsl:when test="contains($outputString,$target)">
        <xsl:value-of select="concat(substring-before($outputString,$target),$replacement)" />
        <xsl:call-template name="globalReplace">
          <xsl:with-param name="outputString" select="substring-after($outputString,$target)" />
          <xsl:with-param name="target" select="$target" />
          <xsl:with-param name="replacement" select="$replacement" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$outputString" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
 
Old October 29th, 2008, 10:57 PM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default

got it but didnt understand

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


why we need to use <FINISH> tag ..

 
Old October 30th, 2008, 03:49 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well you wanted a 'FINISH' element rather than a 'finish' element. If you have more specific rules that state that all elements in a specified set should be should have their names altered to be UPPERCASE then that's possible too. You just said, however, that 'finish' needed to be 'FINISH'.

--

Joe (Microsoft MVP - XML)
 
Old October 30th, 2008, 04:36 AM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default

yep how can we do that

Raj

 
Old October 30th, 2008, 06:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>i want to replace <break></break> with <br></br>
how can i do this.

>i tried using this template

<xsl:template name="SubstringReplace">

Your template is designed to replace strings in the XML content, whereas your requirement is to rename nodes. To rename a node, do

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

(The apply-templates call is not needed, of course, if the element is always empty.)

By the way, please don't delete threads from the forum. There are people getting email notifications of your posting, and if they find the email in their inbox and recognize the problem as one where they would like to know the answer, it's very irritating to find the whole thread has been deleted. It's also irritating to people like me who answer posts to have my work deleted - if we thought you were the only person who needs the information, we wouldn't bother answering at all.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old October 30th, 2008, 08:39 PM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default

oops i am sorry for it,but i have deleted the post where there was no reply.
Anyways sorry






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
replace function keyvanjan Classic ASP Basics 3 May 15th, 2006 12:57 AM
Character replace function? echovue Access 2 December 21st, 2004 01:53 PM
replace function Adam H-W Classic ASP Basics 3 September 20th, 2004 09:09 PM





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