Reputation: 4543
I am writing an xsl for an xml, but I am stuck at &
in my xml.
It shows error like A semi colon character was expected. Error processing resource..
.
Now I want all &
to be replaced with &
. so that it won't show any error.
How can I achieve this?
Is there any alternative way of fixing this problem?
Upvotes: 1
Views: 4737
Reputation: 7980
Not possible with XSL - it operates on well-formed XML input only.
You need to use some search & replace functionality, for instance sed:
sed -i 's:&:&:' yourfile.xml
Or even better:
sed -i 's:&:&:g;s:amp;amp;:amp;:g;' yourfile.xml
which prevents you from replacing correct entity references.
Upvotes: 1
Reputation: 327
So here's an answer. Replace the $input parameter with your input file. Run against any valid XML file - NOT your input file as the input - the script will ignore it.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
exclude-result-prefixes="xs xd"
version="2.0">
<xsl:param name="input" select="'file:/U:/input.txt'"/>
<xsl:template match="/">
<result>
<xsl:analyze-string select="unparsed-text($input)" regex="&">
<xsl:matching-substring><xsl:text>&</xsl:text></xsl:matching-substring>
<xsl:non-matching-substring><xsl:copy-of select="."></xsl:copy-of></xsl:non-matching-substring>
</xsl:analyze-string>
</result>
</xsl:template>
</xsl:stylesheet>
PS - I leave this as a proof of concept rather than a suggested solution to your underlying problem. XSLT is not the best tool for this job (just because all you have is a hammer doesn't mean you should treat everything like a nail).
Upvotes: 1