XSLT 2.0 - Saxon Result-document skip on error? - error-handling

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.

Related

Need to create a function with argument in XSLT 1.0

Need small help in creating a function with parameter and that can be used in my template. Tried many things but nothing worked out. Below is my sample code :
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:math="http://exslt.org/math"
xmlns:func="http://exslt.org/functions"
xmlns:my="http://exslt.org/my"
exclude-result-prefixes="my"
extension-element-prefixes="my math func">
<xsl:output indent="yes" method="xml" />
<xsl:variable name="functionTest11">
<xsl:value-of select="my:calXY('aa')" />
</xsl:variable>
<xsl:template match="/">
<svg xmlns="http://www.w3.org/2000/svg">
<xsl:variable name="functionTest22">
<xsl:value-of select="my:calXY" />
</xsl:variable>
<xsl:comment>functionTest <xsl:copy-of
select="$functionTest22" /></xsl:comment>
</svg>
</xsl:template>
<func:function name="my:calXY">
<xsl:comment>functionHello </xsl:comment>
<xsl:param name="string1" select="''"/>
<func:result>
<xsl:value-of select="concat(10,'|',10)" />
</func:result>
</func:function>
</xsl:stylesheet>
I don't have convenient access (that I know of) to Xalan, which appears to be the XSLT processor you are using. So I cannot test any conjectures about the origin of your problem.
But it would surprise me a great deal if putting an output instruction like xsl:comment before the first (and only) xsl:param were correct. (It surprises me that you don't report any error messages from the stylesheet compiler. But it's clear from the error message you quote that Xalan has not successfully compiled the function.) It's not clear what you are trying to accomplish by having your function return both a comment node and the string value '10|10', but this is probably not the way to accomplish it.
On the assumption that the xsl:comment instruction is an attempt to verify that the function has in fact been evaluated, I'd try something like
<func:function name="my:calXY">
<xsl:param name="string1" select="''"/>
<func:result select="concat(10,'|',10)" />
</
[In an earlier version of this answer, I speculated that func:result is not needed here; my error. It does appear to be needed.]
You ask for a complete stylesheet. The following stylesheet functions as expected when run with xsltproc. (As I noted above, I don't have a convenient way to run Xalan.)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:func="http://exslt.org/functions"
xmlns:my="http://example.org/my"
exclude-result-prefixes="my"
extension-element-prefixes="func">
<xsl:output indent="yes" method="xml" />
<xsl:template match="/">
<xsl:message>Result is <xsl:value-of select="my:dummy('foo')"/></xsl:message>
<output>
<test><xsl:value-of select="my:dummy('bar')"/></test>
<test><xsl:value-of select="my:dummy('baz')"/></test>
</output>
</xsl:template>
<func:function name="my:dummy">
<xsl:param name="s" select="'foo'"/>
<func:result select="concat($s,'|',$s)" />
</func:function>
</xsl:stylesheet>

Date comparison in XSLT using Java

I am using the below code snippet to fetch the current date in XSLT:
<xsl:variable name="currentDate">
<xsl:variable name="datePattern">yyyy-MM-dd'T'HH:mm:ss Z</xsl:variable>
<xsl:value-of select="java:format(java:java.text.SimpleDateFormat.new($datePattern), java:java.util.Date.new())" />
</xsl:variable>
Requirement is, to check if ShippedDate attribute value is equal to today's date, then include <Monitor> element, else exclude.
<Order>
<Monitors>
<Monitor ShippedDate="2015-11-10T00:00:00-05:00"/>
<Monitor ShippedDate="2015-12-03T00:00:00-05:00"/>
</Monitors>
</Order>
Can somebody please help me with what function needs to be used to achieve this?
Requirement is, to check if ShippedDate is equal to today's date, then
include <Monitor> element, else exclude.
I would suggest you do it this way:
XSLT 1.0 (+EXSLT)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Monitors">
<xsl:copy>
<xsl:apply-templates select="Monitor[substring(#ShippedDate, 1, 10) = substring(date:date(), 1, 10)]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Test input:
<Order>
<Monitors>
<Monitor ShippedDate="2015-12-01T00:00:00-05:00"/>
<Monitor ShippedDate="2015-12-02T00:00:00-05:00"/>
<Monitor ShippedDate="2015-12-03T00:00:00-05:00"/>
</Monitors>
</Order>
Result, when applied on December 2, 2015:
<?xml version="1.0" encoding="UTF-8"?>
<Order>
<Monitors>
<Monitor ShippedDate="2015-12-02T00:00:00-05:00"/>
</Monitors>
</Order>
Note:
This requires a processor that supports the EXSLT date:date() extension function (e.g. Xalan or Saxon 6.5);
Only the date component is compared. If you are running the transformation in a time zone that's different from the time zone used in the input, the result may be inaccurate. If you want to also consider the time zone, then there's more work to do.
Added:
As a continuation of this, if none of the ShippedDate satisfies
condition, then no output must be transformed by XSLT.
Try:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Order">
<xsl:variable name="current" select="Monitors/Monitor[substring(#ShippedDate, 1, 10) = substring(date:date(), 1, 10)]"/>
<xsl:if test="$current">
<Order>
<Monitors>
<xsl:copy-of select="$current"/>
</Monitors>
</Order>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

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/

XSLT-1.0 can a variable be used to access to other nodes?

With a simple XML like this
<value>
<num>
<accession>111</accession>
<sequence>AAA</sequence>
<score>4000</score>
</num>
</value>
I want to know if it is possible to access to a particular node from a node previously stored in a variable. The XSLT code is very short and explains better what I want to say
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value/num">
<xsl:variable name="node">
<xsl:copy-of select="current()"/>
</xsl:variable>
<root>
<xsl:copy-of select="$node"/>
</root>
</xsl:template>
</xsl:stylesheet>
So I store the node in the variable "node". Then I can print the contents of the node with $node.
(EDIT) XML output
<root>
<num>
<accession>111</accession>
<sequence>AAA</sequence>
<score>4000</score>
</num>
</root>
What I want to do is to print the contents of a sub-node, like this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value/num">
<xsl:variable name="node">
<xsl:copy-of select="current()"/>
</xsl:variable>
<root>
<xsl:copy-of select="$node/accession"/>
</root>
</xsl:template>
</xsl:stylesheet>
But it gives an error (Component returned failure code: 0x80600008 [nsIXSLTProcessor.transformToFragment]) (check here)
(EDIT) The XML that I would want is
<root>
<accession>111</accession>
</root>
NOTE: The question is not how can I get this output. The question is how, using a variable as in the XSLT provided, can I get this output.
(EDIT:SOLVED)
Actually it is possible, but as pointed out in the comments, the value of a variable has to be assigned with the "select" attribute if a node-set is required. So this code was not working since the variable had a tree fragment instead of a node-set stored in it (read more information here)
Thanks!
Try this:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value">
<root>
<xsl:for-each select="num">
<xsl:variable name="node" select="current()" />
<xsl:copy-of select="$node/accession" />
</xsl:for-each>
</root>
</xsl:template>
</xsl:transform>
Note that I used xsl:transform instead of xsl:stylesheet. Also, consider using version 2.0 instead of 1.0 if you have a compliant processor, it adds a lot of useful features.
I still don't see your need for a variable, though.

XML local-name replacement through XSLT

I want to convert following code
================
<spirit:parameter>
<spirit:name>P3</spirit:name>
<spirit:test_value>8</spirit:test_value>
</spirit:parameter>
</spirit:parameters>
================
into
================
<spirit:parameter>
<spirit:name>P3</spirit:name>
<spirit:glue_value>8</spirit:glue_value>
</spirit:parameter>
</spirit:parameters>
================
I want to do it through XSLT. Please help
Your example input wasn't quite valid, so changed it slightly and added a namespace:
<?xml version="1.0" encoding="UTF-8"?>
<spirit:parameters xmlns:spirit="http://example.com/namespace/spirit">
<spirit:parameter>
<spirit:name>P3</spirit:name>
<spirit:test_value>8</spirit:test_value>
</spirit:parameter>
</spirit:parameters>
You essentially want to copy all nodes except 'spirit:test_value' which needs to be renamed:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:spirit="http://example.com/namespace/spirit">
<xsl:output omit-xml-declaration='yes'/>
<xsl:template match="spirit:parameters|spirit:parameter|spirit:name">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="spirit:test_value">
<spirit:glue_value>
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</spirit:glue_value>
</xsl:template>
</xsl:stylesheet>
The '<xsl:copy-of select="#*"/>' sections there are optional, you'd need those if you want to copy across any attributes from the source XML.