I've been dealing with XSLT 2.0 in the past day, trying to parse a plain text file. apparently I couldn't even get to the part where I actually get it working, at the moment, the xsl doesn't have to do something, just to load properly in saxonb-xslt processor.
XSL:
<xsl:stylesheet xmlns:xsl="http://www开发者_JS百科.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match='/'>
<add_adverts>
<Body>
<Envelope>
<Advert>
<xsl:for-each select="tokenize(unparsed-text('A2.blm'), '\r?\n')">
<fff>?</fff>
</xsl:for-each>
</Advert>
</Envelope>
</Body>
</add_adverts>
</xsl:template>
</xsl:stylesheet>
How I run it:
saxonb-xslt -s:A2.blm -xsl:eraxsl.xsl -o:test.xml
the blm file is a plain text file, first line is: #HEADER# last line is #END# there is a line in it named #DATA# from which I want to parse until the end. each record is separated by ^.
Thanks,
The -s:
parameter to saxon-xslt specifies the source document which has to be in xml format I think. In your example you don't need this source since you specify the file name in your template. The solution would be to remove the source parameter and to specify a named template instead of the match as the starting point:
<xsl:template name="main">
<add_adverts>...<add_adverts>
</xsl:template>
saxonb-xslt -xsl:eraxsl.xsl -o:test.xml -it:main
I do not repro this problem at all -- I have created the "A2.blm" file in the same directory as the XSLT stylesheet. The transformation works as expected.
This error is from the XML parser, not from the XSLT processor. Most probably you have provided as source XML file something that is not a well-formed XML document (or is completely missing).
Most probably the file "A2.blm" cannot be found or accessed -- check well.
In order of this file to be found, it must be in the same directory your stylesheet file is.
From the XSLT 2.0 W3C spec:
"The unparsed-text function reads an external resource (for example, a file) and returns its contents as a string.
The $href argument must be a string in the form of a URI. The URI must contain no fragment identifier, and must identify a resource that can be read as text. If the URI is a relative URI, then it is resolved relative to the base URI from the static context. "
And most importantly:
"Note: If a different base URI is appropriate (for example, when resolving a relative URI read from a source document) then the relative URI should be resolved using the resolve-uriFO function before passing it to the unparsed-text function."
Here is a proof that the rest of your transformation works as intended:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match='/'>
<add_adverts>
<Body>
<Envelope>
<Advert>
<xsl:for-each select="tokenize(., '\r?\n')">
<fff>?</fff>
</xsl:for-each>
</Advert>
</Envelope>
</Body>
</add_adverts>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on this XML document:
<t>1
2
3
</t>
the wanted, correct result is produced:
<add_adverts xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
<Body>
<Envelope>
<Advert>
<fff>?</fff>
<fff>?</fff>
<fff>?</fff>
<fff>?</fff>
</Advert>
</Envelope>
</Body>
</add_adverts>
精彩评论