Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 5th, 2006, 11:02 PM
Registered User
 
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error - Content is not allowed in prolog. --- SOS

Below is my XSLT and my input is a plain text file with a couple of H and Ls'.

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

<xsl:template match="text()">
  <xsl:call-template name="_replace_string">
    <xsl:with-param name="string" select="." />
    <xsl:with-param name="find" select="'H'" />
    <xsl:with-param name="replace" select="'-H-'" />
    <xsl:with-param name="find2" select="'L'" />
    <xsl:with-param name="replace2" select="'-L-'" />
  </xsl:call-template>
</xsl:template>


<xsl:template name="_replace_string">
<xsl:param name="string" select="''"/>
<xsl:param name="find" select="'find'"/>
<xsl:param name="replace" select="'replace'"/>
<xsl:param name="find2" select="'find2'"/>
<xsl:param name="replace2" select="'replace2'"/>
<xsl:choose>
<xsl:when test="contains($string,$find)">
<xsl:value-of select="concat(substring-before($string,$find),$replace)"/>
<xsl:call-template name="_replace_string">
<xsl:with-param name="string" select="substring-after($string,$find)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string,$find2)">
<xsl:value-of select="concat(substring-before($string,$find2),$replace2)"/>
<xsl:call-template name="_replace_string">
<xsl:with-param name="string" select="substring-after($string,$find2)"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>



I get an error "javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRunti meException: Content is not allowed in prolog." when trying to do a simple string replacement in a text file. Output is a text file too..

Your help is much appreciated..

Thanks

 
Old October 6th, 2006, 02:28 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

XSLT 1.0 will only accept XML input files. XSLT 2.0 enables other formats to be used but not as the main input, only via functions such as unparsed-text().

--

Joe (Microsoft MVP - XML)
 
Old October 6th, 2006, 06:52 AM
Registered User
 
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe,

thanks for your reply. As per your advise, I have changed the version tag of <xsl:stylesheet> to "2.0" and changed the main input to <xsl:template match="unparsed-text()". So new XSLT is as follows.. with the mentioned changes, i now get a compilation error

ERROR: 'Syntax error in 'unparsed-text()'.'
FATAL ERROR: 'Could not compile stylesheet'

Could you plz help ?

Thanks



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



<xsl:template name="_replace_string">
<xsl:param name="string" select="''"/>
<xsl:param name="find" select="'find'"/>
<xsl:param name="replace" select="'replace'"/>
<xsl:param name="find2" select="'find2'"/>
<xsl:param name="replace2" select="'replace2'"/>
<xsl:choose>
<xsl:when test="contains($string,$find)">
<xsl:value-of select="concat(substring-before($string,$find),$replace)"/>
<xsl:call-template name="_replace_string">
<xsl:with-param name="string" select="substring-after($string,$find)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string,$find2)">
<xsl:value-of select="concat(substring-before($string,$find2),$replace2)"/>
<xsl:call-template name="_replace_string">
<xsl:with-param name="string" select="substring-after($string,$find2)"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="text()">
  <xsl:call-template name="_replace_string">
    <xsl:with-param name="string" select="."/>
    <xsl:with-param name="find" select="'H'"/>
    <xsl:with-param name="replace" select="'-H-'"/>
    <xsl:with-param name="find2" select="'L'"/>
    <xsl:with-param name="replace2" select="'-L-'"/>
  </xsl:call-template>
</xsl:template>


</xsl:stylesheet>

 
Old October 6th, 2006, 07:02 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need to read up on using XSLT, especially version 2.0. Firstly you need to use a version 2.0 processor, the only real option, in my opinion is Saxon, which you read about at www.saxonica.com. Secondly, as I said, you cannot use non XML as your document's primary source you need to bring the content in in a similar way to using the doucument function in version 1.0. The alternative is to pre-process the text beforehand to a simple XML structure before passing it to the transform.

--

Joe (Microsoft MVP - XML)
 
Old October 6th, 2006, 07:07 AM
Registered User
 
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Attached it the correction i tried, but get an error saying

ERROR: 'Error checking type of the expression 'funcall(unparsed-text, [literal-expr(abc.txt), literal-expr(UTF-8)])'.'
FATAL ERROR: 'Could not compile stylesheet'


thanks for your help




<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text"/>



<xsl:template name="_replace_string">
<xsl:param name="string" select="''"/>
<xsl:param name="find" select="'find'"/>
<xsl:param name="replace" select="'replace'"/>
<xsl:param name="find2" select="'find2'"/>
<xsl:param name="replace2" select="'replace2'"/>
<xsl:choose>
<xsl:when test="contains($string,$find)">
<xsl:value-of select="concat(substring-before($string,$find),$replace)"/>
<xsl:call-template name="_replace_string">
<xsl:with-param name="string" select="substring-after($string,$find)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string,$find2)">
<xsl:value-of select="concat(substring-before($string,$find2),$replace2)"/>
<xsl:call-template name="_replace_string">
<xsl:with-param name="string" select="substring-after($string,$find2)"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="text()">
  <xsl:call-template name="_replace_string">
    <xsl:with-param name="string" select="unparsed-text('abc.txt','UTF-8')"/>
    <xsl:with-param name="find" select="'H'"/>
    <xsl:with-param name="replace" select="'-H-'"/>
    <xsl:with-param name="find2" select="'L'"/>
    <xsl:with-param name="replace2" select="'-L-'"/>
  </xsl:call-template>
</xsl:template>


</xsl:stylesheet>

 
Old October 6th, 2006, 07:07 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You've jumped into the driving seat of a 40-ton truck and you're asking passers-by how to start the engine. The only sane advice is to tell you not to: you need to visit a driving school first. Or in this case, since we're on a Wrox site, find a good book and spend a day reading it.

unparsed-text() can be used in an XPath expression to read a text file, it can't be used in match pattern. Use a named template as the entry point to your transformation, then call unparsed-text() to read the external file into a string.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 6th, 2006, 07:21 AM
Registered User
 
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Mike,

Is this what you meant?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text"/>



<xsl:template name="_replace_string">
<xsl:param name="string" select="unparsed-text(document-uri(.))"/>
<xsl:param name="find" select="'find'"/>
<xsl:param name="replace" select="'replace'"/>
<xsl:param name="find2" select="'find2'"/>
<xsl:param name="replace2" select="'replace2'"/>
<xsl:choose>
<xsl:when test="contains($string,$find)">
<xsl:value-of select="concat(substring-before($string,$find),$replace)"/>
<xsl:call-template name="_replace_string">
<xsl:with-param name="string" select="substring-after($string,$find)"/>
<xsl:with-param name="find" select="$find"/>
<xsl:with-param name="replace" select="$replace"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string,$find2)">
<xsl:value-of select="concat(substring-before($string,$find2),$replace2)"/>
<xsl:call-template name="_replace_string">
<xsl:with-param name="string" select="substring-after($string,$find2)"/>
<xsl:with-param name="find2" select="$find2"/>
<xsl:with-param name="replace2" select="$replace2"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="text()">
  <xsl:call-template name="_replace_string">
    <xsl:with-param name="string" select="''"/>
    <xsl:with-param name="find" select="'H'"/>
    <xsl:with-param name="replace" select="'-H-'"/>
    <xsl:with-param name="find2" select="'L'"/>
    <xsl:with-param name="replace2" select="'-L-'"/>
  </xsl:call-template>
</xsl:template>


</xsl:stylesheet>

Thanks

 
Old October 6th, 2006, 07:25 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Are you sure that you're using an XSLT processor that supports XSLT 2.0? Which processor is it?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 6th, 2006, 07:42 AM
Registered User
 
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi mike,

 I am using Sun's transformer javax.xml.transform.Transformer which is the JAXP transformer. But when I replaced the unparsed-text() with a hardcoded string "HHHLL", I get the error - ERROR: 'Content is not allowed in prolog.'

  Does this mean that my transformer doesn't support XSlt 2.0?

Thanks a lot for your help



 
Old October 6th, 2006, 07:48 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Question to self, does anyone actually read the answers I post?


--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
I am getting this error- Operaton is not allowed Shuchik Classic ASP Basics 10 August 24th, 2007 01:58 AM
Using basic C code in Prolog Ruman29 C++ Programming 0 March 11th, 2007 01:22 PM
Error HTTP 405 - Resource not allowed shefali Classic ASP Basics 2 February 20th, 2006 06:47 PM
error HTTP 405 - Resource not allowed hastikeyvan Dreamweaver (all versions) 1 January 12th, 2006 12:11 PM
3219 Error: Operation Not Allowed in this Context dolfandave VB Databases Basics 0 May 17th, 2005 10:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.