This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: replace &lt; and &gt; with < and >


Thanks a lot. But I tried and it still won't work. Here is the odd thing.

I use a very simple java transformer and run the code for my xml file and my stylesheet, it works just fine. But when I run it through servlet, the transformer just failed. Here is the sample of the xml file:

<files>
<file db="db1" name="/records/123456" />
</files>

And the stylesheet is :

<xsl:template match="/files">
<xsl:for-each select='file'>
<xsl:variable name="record" select="util:parseXml(@name)"/>
<xsl:value-of select="$record/item"/>
</xsl:for-each>
</xsl:template>

The function util:parseXml( ) takes the string which is a key of a xml file, grab it from database and make it a xml string, and then use oracle xml parser to convert it to a nodelist.

But I don't understand why it works when I run from command line but the transformer failed in the servlet.

Thanks a lot for your help.

Ming
At 07:44 PM 9/19/2002 -0600, you wrote:
Ming Yu wrote:
> The xml file is returned from a method is in a string format, and the xml
> file seems to encode all < and > using &lt; and &gt;. So, what I need to do
> is to convert that xml file from a string to a xml nodelist so I can access
> all the elements.
>
> Then, first I need to replace all &lt; and &gt; to < and > and then use a
> method I wrote called parseXml which takes a string as parameter and
> convert it to a nodelist.
>
> But I'm stuck on the first step which is converting all &lt; and &gt; to <
> and >.
>
> Here is a sample of my xml file in a STRING:
>
> &lt;record&gt;
> &lt;items&gt;
> &lt;/items&gt;
> &lt;/record&gt;

I am guessing that despite what you see in the string, your XPath string
object actually does contain "<record>", and this only appears as
"&lt;record&gt;" when you copy and serialize it using the XML or HTML output
method.

If so, then you only need to register your parseXml method as an extension
function, using the mechanism provided by your XSLT processor vendor, then
pass the string to it as-s.

If my guess is wrong, then you can use a recursive function to replace the
substrings. See http://skew.org/xml/stylesheets/replace/replace.xsl for the
SubstringReplace template (this is also in the FAQ but the FAQ site seems to
be down today).

To replace &lt;, &gt;, and &amp; you'll have to call it 3x like this:

<xsl:variable name="betterXml">
<xsl:call-template name="SubstringReplace">
<xsl:with-param name="stringIn">
<xsl:call-template name="SubstringReplace">
<xsl:with-param name="stringIn">
<xsl:call-template name="SubstringReplace">
<xsl:with-param name="stringIn">
<xsl:call-template name="SubstringReplace">
<xsl:with-param name="stringIn" select="$theXml"/>
<xsl:with-param name="substringIn" select="'&amp;lt;'"/>
<xsl:with-param name="substringOut" select="'&lt;'"/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="substringIn" select="'&amp;gt;'"/>
<xsl:with-param name="substringOut" select="'&gt;'"/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="substringIn" select="'&amp;amp;'"/>
<xsl:with-param name="substringOut" select="'&amp;'"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:variable>

Then pass string($betterXml) to your extension function.

- Mike
____________________________________________________________________________
mike j. brown | xml/xslt: http://skew.org/xml/
denver/boulder, colorado, usa | resume: http://skew.org/~mike/resume/

XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]