0
votes

I am trying to replace a string in an element that contains mixed elements, but as an XSLT newbie, I am not sure how to do it. I am required to use XSLT 1.0 and am not sure if replacing a string with an element value that contains mixed elements is possible or legal in XSLT 1.0. I did not include the actual xml and xslt files as they are too large to post, so I came up with some examples of what I am trying to accomplish.

Here is an example XML file that I am trying to transform:

<?xml version="1.0"?>
<test>
  <testing>The author named "<sub name="bob"/>" who wrote
             <book name="Over the river" /> is STATUS.
  </testing>
</test>

Here is my example XSLT file:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xhtml="http://www.w3.org/1999/xhtml" >

 <xsl:template name="find-and-replace">
   <xsl:param name="text"/>
   <xsl:param name="replace"/>
   <xsl:param name="with"/>
   <xsl:choose>
     <xsl:when test="contains($text,$replace)">
       <xsl:value-of select="substring-before($text,$replace)"/>
       <xsl:value-of select="$with"/>
       <xsl:call-template name="find-and-replace">
         <xsl:with-param name="text" select="substring-after($text,$replace)"/>
         <xsl:with-param name="replace" select="$replace"/>
         <xsl:with-param name="with" select="$with"/>
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="$text"/>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>

 <xsl:template match="testing">
   <xsl:element name="testing">
     <xsl:call-template name="find-and-replace">
       <xsl:with-param name="text" select="."/>
       <xsl:with-param name="replace" select="'STATUS'"/>
       <xsl:with-param name="with" select="'LIVING'"/>
     </xsl:call-template>
   </xsl:element>
 </xsl:template>

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

</xsl:stylesheet>

The transformed example xml file looks like this:

<?xml version="1.0"?>
<test>
  <testing>The author named "" who wrote  is LIVING.
  </testing>
</test>

which makes sense as select="." does not output the sub or book nodes like regular text. I would like for the transformed example xml file to look like this:

<?xml version="1.0"?>
<test>
  <testing>The author named "<sub name="bob"/>" who wrote <book name="Over the river" /> is LIVING.
  </testing>
</test>

Is this even possible with XSLT 1.0? If so, how can I accomplish this?

Thank you for your understanding and your hints!

1

1 Answers

0
votes

change

<xsl:template match="testing">
   <xsl:element name="testing">
     <xsl:call-template name="find-and-replace">
       <xsl:with-param name="text" select="."/>
       <xsl:with-param name="replace" select="'STATUS'"/>
       <xsl:with-param name="with" select="'LIVING'"/>
     </xsl:call-template>
   </xsl:element>
</xsl:template>

to

<xsl:template match="testing/text()">
        <xsl:call-template name="find-and-replace">
            <xsl:with-param name="text" select="."/>
            <xsl:with-param name="replace" select="'STATUS'"/>
            <xsl:with-param name="with" select="'LIVING'"/>
        </xsl:call-template>
</xsl:template>