This question is an extension of this one. I have many values, as seen below:
<myStuff>
<elem1>asdf</elem1>
<elem2>foo bar</elem2>
<elem3>foo bar foo</elem3>
<elem4>foofoo</elem4>
</myStuff>
I've been doing a copy-of
over MOST the elems (the select
in the copy-of
is very specific) and then doing a find-and-replace on the resulting XML, but I'd like to combine the two. In most of the situations where I've applied this, I would replace anything that said <xsl:value-of select="whatever">
with <xsl:apply-templates select="whatever">
and use the below template:
<xsl:template match="*">
<xsl:variable name="rep1">
<xsl:choose>
<xsl:when test='matches(., ".*foo.*")'>
<xsl:value-of select='replace(., "foo", "qwerty")' />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='./text()' />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="rep2">
<xsl:choose>
<xsl:when test='matches(., ".*bar.*")'>
<xsl:value-of select='replace($rep1, "bar", "myBar")' />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$rep1' />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$rep2" />
</xsl:template>
I would like to use a similar template to replace the copy-of
in my code (because I'm making the same replacements as in my other files, so I could use the same template), but I'm unsure of how to do this.
The output would look like this:
<myOtherStuff>
<elem1>asdf</elem1>
<elem2>qwerty myBar</elem2>
<elem3>qwerty myBar qwerty</elem3>
<elem4>qwertyqwerty</elem4>
</myOtherStuff>
All help is appreciated and thanks in advance!
With another approach, this stylesheet:
Output:
Also, this input (from comments):
Output:
Note: This RegExp union perform the replacement all at once. This may differ of sequencely
fn:replace
calls: suppose this replacement targets "A" -> "B" and "B" -> "C" on this string "AB", union replacement should output "BC" and sequencely calls output "CC".Note 2: With regard to matching order, do note that RegExp union follows its own rules (see specs, more specific
If two alternatives within the supplied $pattern both match at the same position in the $input string, then the match that is chosen is the first.
) and sequencelyfn:replace
calls follows strictly user defined order.This transformation:
when applied on the provided XML document:
produces the wanted, correct result: