xsl-list
[Top] [All Lists]

Re: Find and change an attribute in a Processing Instruction

2006-01-30 11:47:51
Hello All,

I modified an example in Michael's XSLT 2nd Edition, page 552. Here is my XML.

<?xml version="1.0" encoding="UTF-8"?>
<?Fm Condition Comment Red NO_OVERRIDE hide?>
<?Fm Condition Prolog PANTONE%20648-50%20CVP NO_OVERRIDE show?>
<?Fm Condition Internal Black DOUBLE_UNDERLINE hide?>
<concept>
 <title>Sample Title</title>
 <?Fm Condstart Prolog?>
 <prolog><author>bernard</author></prolog>
 <?Fm Condend Prolog?>
 <conbody>
   <section><p>Some sample text.</p>
   <draft-comment  author="Rick" importance="high">
   <p>an author comment</p>
   </draft-comment>
   <?Fm Condend Comment?>
   <p>Some more sample text.</p>
   </section>
  </conbody>
</concept>

Here is my stylesheet:

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

<xsl:variable name="find" select="'hide'"/>
<xsl:variable name="replace" select="'show'"/>

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

<xsl:template match="*">
 <xsl:copy>
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates/>
 </xsl:copy>
</xsl:template>

<xsl:template match="/processing-instruction()">
 <xsl:call-template name="do-replace">
   <xsl:with-param name="text" select="."/>
 </xsl:call-template>
</xsl:template>

</xsl:stylesheet>

Here is my output:

<?xml version="1.0" encoding="UTF-8"?>Condition Comment Red NO_OVERRIDE hideCondition Prolog PANTONE%20648-50%20CVP NO_OVERRIDE showCondition Internal Black DOUBLE_UNDERLINE hide<concept>
 <title>Sample Title</title>

 <prolog><author>bernard</author></prolog>

 <conbody>
   <section><p>Some sample text.</p>
   <draft-comment author="Rick" importance="high">
   <p>an author comment</p>
   </draft-comment>

   <p>Some more sample text.</p>
   </section>
  </conbody>
</concept>

There are three problems that I see:

1) The "hide" strings in the processing instructions are not being replaced by "show".
2) I am loosing the <? ?> delimiters on the processing instructions.
3) Lower level processing instructions are not getting written to the output (e.g. <?Fm Condstart Prolog?>).

One other thing I noticed: in order to declare non-numeric variables, I have to use two pairs of quotes ("''"). I couldn't find this documented anywhere.

I am using Saxon 8.5, but my stylesheet needs to be XSLT 1 compatible. Thanks in advance for any help you can give.

Rick Quatro
Carmen Publishing
585-659-8267
www.frameexpert.com



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--