View Single Post
  #4 (permalink)  
Old December 25th, 2003, 02:14 AM
armmarti armmarti is offline
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Quote:
quote:Originally posted by AyatKh
 For example I need to replace "Barnes And Noble" with "Test" in this code. what is the best way to search for that text? Which object I should use?

<book genre="novel" ISBN="1-861001-57-5">
  <title>Pride And Prejudice</title>
  <seller>Barnes And Noble</seller>
  <price>$45</price>
</book>

You can achieve this by processing the DOM tree(by parsing the document using TrAX and then manipulate the DOM tree using Java, for example). Another approach is a stylesheet:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="what" select="'Barnes And Noble'"/>
    <xsl:param name="by-what" select="'Test'"/>
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="node() | @*">
        <xsl:choose>
            <xsl:when test="self::text()">
                <xsl:variable name="this-text" select="self::text()"/>
                <xsl:choose>
                    <xsl:when test="contains($this-text, $what)">
                        <xsl:call-template name="replace">
                            <xsl:with-param name="str" select="$this-text"/>
                            <xsl:with-param name="what" select="$what"/>
                            <xsl:with-param name="by-what" select="$by-what"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="node() | @*"/>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="replace">
        <xsl:param name="str"/>
        <xsl:param name="what"/>
        <xsl:param name="by-what"/>

        <xsl:choose>
            <xsl:when test="not($str)"/>
            <xsl:when test="starts-with($str, $what)">
                <xsl:value-of select="$by-what"/>
                <xsl:call-template name="replace">
                    <xsl:with-param name="str" select="substring($str, string-length($what)+1)"/>
                    <xsl:with-param name="what" select="$what"/>
                    <xsl:with-param name="by-what" select="$by-what"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="substring($str, 1, 1)"/>
                <xsl:call-template name="replace">
                    <xsl:with-param name="str" select="substring($str, 2)"/>
                    <xsl:with-param name="what" select="$what"/>
                    <xsl:with-param name="by-what" select="$by-what"/>                
                </xsl:call-template>    
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
This stylesheet replaces all occurences of string kept in variable $what with a string kept in variable $by-what in all text nodes.

Regards,
Armen
Reply With Quote