 |
| 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
|
|
|
|

October 5th, 2006, 11:02 PM
|
|
Registered User
|
|
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 6th, 2006, 02:28 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

October 6th, 2006, 06:52 AM
|
|
Registered User
|
|
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|

October 6th, 2006, 07:02 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

October 6th, 2006, 07:07 AM
|
|
Registered User
|
|
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|

October 6th, 2006, 07:07 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

October 6th, 2006, 07:21 AM
|
|
Registered User
|
|
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 6th, 2006, 07:25 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

October 6th, 2006, 07:42 AM
|
|
Registered User
|
|
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 6th, 2006, 07:48 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Question to self, does anyone actually read the answers I post?
--
Joe ( Microsoft MVP - XML)
|
|
 |