Question

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!

Was it helpful?

Solution

With another approach, this stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:variable name="vReps" as="element()*">
        <rep target="foo" newval="qwerty"/>
        <rep target="bar" newval="myBar"/>
    </xsl:variable>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:analyze-string select="." 
         regex="{string-join($vReps/concat('(',@target,')'),'|')}">
            <xsl:matching-substring>
                <xsl:value-of select="$vReps[@target=current()]/@newval"/>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:template>
</xsl:stylesheet>

Output:

<myStuff>
    <elem1>asdf</elem1>
    <elem2>qwerty myBar</elem2>
    <elem3>qwerty myBar qwerty</elem3>
    <elem4>qwertyqwerty</elem4>
</myStuff>

Also, this input (from comments):

<myStuff>
    <elem>asdfooasdf</elem>
</myStuff>

Output:

<myStuff>
    <elem>asdqwertyasdf</elem>
</myStuff>

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 sequencely fn:replace calls follows strictly user defined order.

OTHER TIPS

This transformation:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:my="my:my" >
 <xsl:output omit-xml-declaration="yes"/>

  <xsl:variable name="vReps" as="element()*">
   <rep target="foo" newval="qwerty"/>
   <rep target="bar" newval="myBar"/>
  </xsl:variable>

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

    <xsl:template match="text()">
      <xsl:sequence select="my:myMultiRep(., $vReps)"/>
    </xsl:template>

    <xsl:function name="my:myMultiRep" as="xs:string">
      <xsl:param name="pText" as="xs:string"/>
      <xsl:param name="pReps" as="element()*"/>

      <xsl:sequence select=
       "if(not($pReps))
         then $pText
         else my:myMultiRep(replace($pText, $pReps[1]/@target, $pReps[1]/@newval),
                            subsequence($pReps, 2)
                           )
       "/>
    </xsl:function>
</xsl:stylesheet>

when applied on the provided XML document:

<myStuff>
    <elem1>asdf</elem1>
    <elem2>foo bar</elem2>
    <elem3>foo bar foo</elem3>
    <elem4>foofoo</elem4>
</myStuff>

produces the wanted, correct result:

<myStuff>
    <elem1>asdf</elem1>
    <elem2>qwerty myBar</elem2>
    <elem3>qwerty myBar qwerty</elem3>
    <elem4>qwertyqwerty</elem4>
</myStuff>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top