Kirk Strobeck
Kirk Strobeck

Reputation: 18579

XSLT select all text

I want to select all text and do a search and replace.
I want to turn all dashed into non-breaking dashes.

I am using this template for the search and replace part,
now I just need to run all text thru it..

Upvotes: 2

Views: 1045

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

I don't know what do you mean by "non-breaking dash", but here is a simple solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="vRep" select="'&#8212;'"/>

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

 <xsl:template match="text()">
  <xsl:value-of select="translate(.,'-', $vRep)"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any XML document, the result is the same document in which any '-' is replaced by whatever is specified as the value of the global parameter $vRep.

For example, when applied on this XML document:

<a>
 <b>Hi - hi</b>
 - - -
 <c>
   <d>Wow... - cool</d>
 </c>
 - - -
</a>

the result is:

<a>
  <b>Hi — hi</b>
 — — —
 <c><d>Wow... — cool</d></c>
 — — —
</a>

Explanation: Use of the identity rule, overriden by a template matching any text node, and translating any '-' character in it to the character contained in $vRep -- by using the standard XPath function translate().

Upvotes: 3

Related Questions