Handling 0x19 in XSLT 1.0 - xslt-1.0

I have encountered a problem when input xml contains the character 0x19. I have created a demo xslt to reproduce the issue.
My demo xslt looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="param1"/>
<xsl:template match="/">
<Value>
<xsl:value-of select="$param1"/>
</Value>
</xsl:template>
</xsl:transform>
I am passing the character 0x19 as param1. The below output gets generated.
<Value></Value>
which is an invalid xml. How can I get it right?

You are correct that
<Value></Value>
is not well-formed XML 1.0 - XML 1.0 does not allow any control characters below U+0020 except U+0009 (tab), U+000A (LF) and U+000D (CR), not even when expressed as numeric character references, so it is simply not possible to include that character in an XML 1.0 document. The processor is wrong to produce that output, it should raise an error to complain that you've tried to insert an illegal character in the output.
However it is well formed XML 1.1, which allows control characters as &# references but not as literals. If your processor supports this (and the donwnstream components that will be receiving your output support it too) then it may be sufficient to add version="1.1" to the xsl:output instruction
<xsl:output method="xml" indent="yes" version="1.1"/>
to tell it to output XML 1.1 instead of XML 1.0.

Related

XSLT 1 - How can I replace '&' with '&' in XML file?

I need to find solution to fix by using XSLT 1! Most of sent XML files well formatted and someone make mess by adding characters (& < >. . .). Any way to do replace this on my side? I tried XSLT 2 and Replace function does not work as I use XSLT processor from Microsoft
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:saxon="http://saxon.sf.net/"
exclude-result-prefixes="xs saxon"
version="2.0">
<xsl:param name="path" select="'file:///E:/foo.xml'"></xsl:param>
<xsl:template match="/">
<xsl:copy-of select="unparsed-text($path)"></xsl:copy-of>
<xsl:copy-of select="saxon:parse(replace(unparsed-text($path), '&', '&amp;'))"/>
</xsl:template>
</xsl:stylesheet>
Any other suggestion how to solve this issue. for example I have input XML file like:
<?xml version="1.0" encoding="UTF-8"?>
<name>Stack & Exchange</name>
And is fail on '&' character.
Please advice!
Thank you
Conclusion
Two observations
XSLT requires at least a well formed XML document at the input, so I can't use it to correct invalid XML
(it is an XML transformation language)
in order to use replace or escape invalid characters of XML on input I need to make sure that I use an XSLT 2.0 processor
(I use Microsoft processor XSLT 1.0)
I see two options
If I receive an error on input, investigate and validate manually and send back the error message. - THIS IS I TRIED TO AVOID! (Use text tools like notepad++, excel to find an issue).
write a correcting parser in the .net language to fix before loading as XML

XSLT 2.0 - Saxon Result-document skip on error?

Having file like this :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:import href="file0.xsl"/>
<xsl:import href="file1.xsl"/>
<xsl:import href="file2.xsl"/>
<xsl:output indent="yes"/>
<xsl:template match="/">
<root>
<xsl:result-document href="../xml/file0.xml" method="xml">
<xsl:call-template name="file0"/>
</xsl:result-document>
<xsl:result-document href="../xml/file1.xml" method="xml">
<xsl:call-template name="file1"/>
</xsl:result-document>
<xsl:result-document href="../xml/file2.xml" method="xml">
<xsl:call-template name="file2"/>
</xsl:result-document>
</root>
</xsl:template>
</xsl:stylesheet>
Q: I'm wandering if there is a way to continue transformation if one of the result-document elements fail to execute.
e.g. file1.xsl throws an error in template file1, will the file2 template be executed???
There's no try/catch capability in XSLT 2.0 or in Saxon-HE. The facility is present in XSLT 3.0 (and as a Saxon extension to XSLT 2.0), but in both cases, that's not available in the free version of the product.

XSLT Data Map with the character < in the data

I'm writing an XSLT data map from one XML schema to another XML schema. The target schema has a String field that will have a copy of the original XML in it. I know that I must convert the control characters in the XML to < and >. I have done that. The issue I'm having is that the data in the XML also has < and > in it as well. This causes problems because I'm sending the XML to a WCF-SQL adapter and when it hits the < in my data it attempts to convert that to < causing XML validation failure.
Here is a snippet of my XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="*" mode="serialize">
<xsl:text><</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
<xsl:apply-templates mode="serialize"/>
<xsl:text></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</xsl:template>
<xsl:template match="/">
<proc_Insert xmlns="http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo">
<StagingXML>
<xsl:variable name="nodestring">
<xsl:apply-templates select="*" mode="serialize"/>
</xsl:variable>
<xsl:value-of select="$nodestring"/>
</StagingXML>
</proc_Insert>
</xsl:template>
</xsl:stylesheet>
You need to quote the content, a truely ancient bit of code (predating xslt 1.0 I think:-) that does that is to be found
http://www.openmath.org/cdfiles2/xsl/verb.xsl
although there are newer versions around as well/

Getting particular substring using xslt1.0

I have a string as below.
<freeForm>
<text>mnr.getValue().put("xyz","pqr");</text>
</freeForm>
From the above xml portion i need to get the string xyz.
Please provide pointers to achieve the same using xslt1.0.
Use this XPath expression:
substring-before(
substring-after(/*/*, &apos;"&apos;),
&apos;"&apos;
)
Here is a short, complete XSLT transformation that evaluates this XPath expression and outputs the result of evaluating it:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select=
'substring-before(
substring-after(/*/*, &apos;"&apos;),
&apos;"&apos;
)'/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<freeForm>
<text>mnr.getValue().put("xyz","pqr");</text>
</freeForm>
the wanted, correct result is produced:
xyz

XSLT 1.0 amount fields have to have at least 2 decimals

I have two xmls.There is a amount field which can contains values like 54.2,54.23,54.234,54.234567.
Would someone please tell me how can I make sure that atleast two decimal places will appear in the output xml.Currently 54.2 gets converted to 54,2 , but I want it to be 54,20
You can use the format-number() function to convert a number into a string in a given format.
At least two decimal places will be appeared if you use this "#.00##########" format string.
If you have an xml file which can contains values like 54.2,54.23,54.234,54.234567:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<price>54.2</price>
<price>54.23</price>
<price>54.234</price>
<price>54.234567</price>
</catalog>
You can convert the numbers with an xslt like this to get at least two decimal places
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="/catalog/price">
<xsl:value-of select='format-number(., "#.00##########")'/><br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Then the output:
54.20
54.23
54.234
54.234567