EDIT: [started with a character replacement and I ended up looking for a replacement string using Dimitre Novachev and Roland Buman
I think the code examples are sufficient to explain the requirements.
This is an example XML:
<root> <node1>text node</node1> <node2>space between the text</node2> <node3> has to be replaced with $</node3> </root>
This is the result that I expect:
<root> <node1>text$node</node1> <node2>space$between$the$text</node2> <node3>$has$to$be$replaced$with$$</node3> </root>
I tried writing XSLT code that does not show the required output.
This is the code:
<xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="text()[.!='']"> <xsl:call-template name="rep_space"> <xsl:with-param name="text" select="."/> </xsl:call-template> </xsl:template> <xsl:template name="rep_space"> <xsl:param name="text"/> <xsl:variable name="temp" select="'6'"/> <xsl:choose> <xsl:when test="contains(text,'2')"> <xsl:call-template name="rep_space"> <xsl:with-param name="text" select="concat((concat(substring-before(text,' '),temp)),substring-after(text,' '))"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="text"/> </xsl:otherwise> </xsl:choose> </xsl:template>
translate (., '', '$') works, but not to a satisfactory degree .. my questions are: .. what if it's a string instead of a character? I mean, suppose I intend to replace '' with "% 20"? And one more case. What if the input XML is not "Pretty Print XML", then all the space that appears in the XML is replaced with "$".
Enough to print XML is a file with the corresponding indentation (usually my input XML files never have this):
another node is @ low
You may notice that there are no whitespace in front of the <new> <test> nodes, but they are actually indented correctly (using Altova XMLSPY we can give a simple command in the editing menu. Any XML files for "pretty print XML") .
Where, as in the example below.
<new> <test>one more node</test> <test2> <child>this is @ lower level</child> </test2> </new>
There are spaces before all start tags. <child> tag has more spaces to <test2> node ..
With the second xml sample .. all whitespace characters are replaced with " %20 " .. so the output will be ...
<new> %20%20<test>one%20more%20node</test> %20%20<test2> %20%20%20%20<child>this%20is% 20@ %20lower%20level</child> %20%20</test2> </new>
Of course, this is not expected ..
Solutions sent by Dimitre Novatchev and Roland Bouman can also replace a string with another string, changing the parameters passed to the called template.
It was a great training @Dimitre, @Roland, I am very grateful and Thank you guys ..
Hi,
infant pro.