 |
| 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
|
|
|
|

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

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

October 29th, 2008, 03:19 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

October 29th, 2008, 10:57 PM
|
|
Authorized User
|
|
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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 ..
|
|

October 30th, 2008, 03:49 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

October 30th, 2008, 04:36 AM
|
|
Authorized User
|
|
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
yep how can we do that
Raj
|
|

October 30th, 2008, 06:43 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>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
|
|

October 30th, 2008, 08:39 PM
|
|
Authorized User
|
|
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
oops i am sorry for it,but i have deleted the post where there was no reply.
Anyways sorry
|
|
 |