As per following URL I enabled the XSLT 2.0 support how ever the looks like the TryIt service is not working when I do that.
Achala Blog Entry
I get following error when I try to use TryIt service.
[2012-09-10 11:47:03,491] INFO - CarbonAuthenticationUtil 'admin' logged in at [2012-09-10 11:47:03,0491] from IP address 0:0:0:0:0:0:0:1%0
Warning: at xsl:stylesheet on line 32 of :
Running an XSLT 1.0 stylesheet with an XSLT 2.0 processor
Warning: on line 286 of :
The attribute axis starting at a namespace() node will never select anything
Error on line 1603 of :
XPTY0004: Required item type of second operand of '|' is node(); supplied value has item
type xs:string
[2012-09-10 11:47:09,515] ERROR - Util Required item type of second operand of '|' is node(); supplied value has item type xs:string
; SystemID: ; Line#: 1603; Column#: -1
net.sf.saxon.trans.DynamicError: Required item type of second operand of '|' is node(); supplied value has item type xs:string
Is there any workaround so that we can use XSLT 2.0 and TryIt both with WSO2 ESB 4.0.3.
Hoping for answer.
thanks
Abhijit
You need to replace the standard XSLT processor by a newer one being able to process XSLT 2.0 like documented in the Blog solution for WSO2 ESB 4.0.3:
Remove xalan-2.7.0.wso2v1.jar in ESB_HOME/lib/endorsed/
Remove saxon-8.9.0.wso2v1.jar from ESB_HOME/repository/components/plugins
Copy the saxon9he.jar to lib/endorsed and start the server.
SAXON 9 you can download here
Be aware that you'll get the SOAP envelope and body also as Input to your XSLT, so maybe you need to have the xsl:template for these elements too:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="/soapenv:Envelope|soapenv:Body">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
...
The latest WSO2 ESB (v4.5.0) supports XSLT 2.0 [1], via Saxson.
[1] http://mail.wso2.org/mailarchive/announce/2012-September/000026.html
Regards,
/Nuwan
Related
Need to read a service response inside xslt which is of format :
servicedata={"statusCode":200,"loginMessage":"Welcome User"}
its key value pair, where i can get the value of 'servicedata' but further need to get the status code value for some condition checking.
Using version 2.0 for the xslt
read few questions which suggested to use parse-json(), json-to-xml with 3.0 version of xslt, but didn't help.
Question: first using version 2.0 only can I retrieve the value for the 'statusCode' and how, if not guide me how to achieve it.
If you can move to Saxon 9.7 HE then you could use json-to-xml as follows for instance:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="xs math"
version="3.0">
<xsl:template match="data">
<xsl:value-of select="json-to-xml(substring-after(., 'servicedata='))//*[#key = 'statusCode']" xpath-default-namespace="http://www.w3.org/2005/xpath-functions"/>
</xsl:template>
</xsl:stylesheet>
which for the input
<data>servicedata={"statusCode":200,"loginMessage":"Welcome User"}</data>
outputs 200.
I have not checked whether earlier versions of Saxon 9 also support that.
I need to call the vbscript externally inside the xsl. I have written a sample xml and xsl when I validate the xml against xsl in have an error of cannot create ActiveX Component.
Next I have transformed the xml against the xsl using Altova but I found XML transformation failed due to following error: Function not in namespaceerror in xpath expression,Function not in namespace.
I have inculded the xml below
XML:
<LOOP_ID>
<ID LINE="1" ID00="ISA" ISA01="00" ID02="" ID03="12" ID04="" ID05="11" ID06="111111" ID07="ZZ" ID08="11111" ID09="121005" ID10="1759" ID11="^" ID12="00501" ID13="005926056" ID14="0" ID15="P" ID16=""/>
</LOOP_ID>
xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://mycompany.com/mynamespace">
<msxsl:script language="VBScript" implements-prefix="user">
Function ValidDateFormat(sDateValue)
Dim Test As DateTime
If DateTime.TryParseExact(datetime, sFormat, New CultureInfo("en-US"), DateTimeStyles.None, Test) = True Then
Return "t"
Else
Return "f"
End If
End Function
Function checkDateLessthanCurrID(sValue)
Dim Test As DateTime
If DateTime.TryParseExact(sDate, "yyMMdd", New CultureInfo("en-US"), DateTimeStyles.None, Test) AndAlso Test < DateTime.Now Then
Return "t"
Else
Return "f"
End If
End Function
Function checkDateLessthanCurr(sValue)
Dim Test As DateTime
If DateTime.TryParseExact(datetime, "MM:dd:yyyy", New CultureInfo("en-US"), DateTimeStyles.None, Test) AndAlso Test < DateTime.Now Then
Return "t"
Else
Return "f"
End If
End Function
</msxsl:script>
<xsl:output method="text" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:apply-templates select="/LOOP_ID"/>
</xsl:template>
<xsl:template match="/LOOP_ID">
<xsl:if test="ID/#ID09 !=''">
<xsl:if test="user:checkDateLessthanCurrISA(string(ID/#ID09))='t'">
</xsl:if>
</xsl:if>
I need to validate the date in the attribute given in xml. But I cannot validate showing error cannot find the namespace.
I have also created a external dll for the project and registered using regasm But I cannot access the dll inside my xsl.
Can any one help me to solve the issue?
Your first error:
cannot create ActiveX Component
means that your code was actually executed, but failed, due to a missing ActiveX reference.
You didn't state it, but most likely you received this by using MSXML, because that is the only XSLT compiler I know of that uses ActiveX. It is also used in Internet Explorer.
Your second error:
Next I have transformed the xml against the xsl using Altova but I found XML transformation failed due to following error: Function not in namespaceerror in xpath expression,Function not in namespace.
is strange. If I run it with Altova (I use the 2013 Community version) using the /xslt commandline switch to turn off XSLT 2.0 backwards compatibility processing, it tries to parse the script block and returns the following:
Script Compile Error(s) (relative to script begin):
Line 3, Character 0: 'datetime' is a type and cannot be used as an expression.
Line 3, Character 0: 'sFormat' is not declared. It may be inaccessible due to its protection level.
Line 3, Character 0: Type 'CultureInfo' is not defined.
Line 3, Character 0: 'DateTimeStyles' is not declared. It may be inaccessible due to its protection level.
Line 12, Character 0: 'sDate' is not declared. It may be inaccessible due to its protection level.
Line 12, Character 0: Type 'CultureInfo' is not defined.
Line 12, Character 0: 'DateTimeStyles' is not declared. It may be inaccessible due to its protection level.
Line 21, Character 0: 'datetime' is a type and cannot be used as an expression.
Line 21, Character 0: Type 'CultureInfo' is not defined.
Line 21, Character 0: 'DateTimeStyles' is not declared. It may be inaccessible due to its protection level.
This suggests that the code is not correct. I think the code uses VBScript and the classes you seem to be using are VB.NET.
When running the code against Microsoft's .NET version of XSLT 1.0, I receive the similar errors as above.
It turns out that Microsoft's script parser does not take your code as VBScript (which is ActiveX), but as a VB.NET script. Which is OK, as your code looks like VB.NET.
However, it is literally full of errors. I am not going to fix every error here, but here's a shortened version of your code that runs correctly on both Altova and Microsoft XSLT versions.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://mycompany.com/mynamespace">
<xsl:strip-space elements="*"/>
<msxsl:script language="VBScript" implements-prefix="user">
Function ValidDateFormat(sDateValue As String) As String
Dim Test As DateTime
Dim sFormat As String = "MM-dd-YY"
If DateTime.TryParseExact(sDateValue, sFormat, New Globalization.CultureInfo("en-US"), Globalization.DateTimeStyles.None, Test) = True Then
Return "true"
Else
Return "false"
End If
End Function
</msxsl:script>
<xsl:output method="text" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="ID[#ID09 !='']">
<xsl:value-of select="user:ValidDateFormat(string(#ID09))"/>
</xsl:template>
</xsl:stylesheet>
The above returns true or false. I suggest you first try the code inside Visual Studio before you try it with XSLT, because inside XSLT it is quite hard to debug.
On mxsxl:script
In my previous post I was incorrect to assume that msxsl:script was not supported by Altova. Martin Honnen corrected me in the comments. It is supported and it looks like it uses the same code provider underneath as Microsoft uses.
Your original error was an ActiveX error. If you want to run your code with an ActiveX XSLT processor, you will need to do a few more things to get it running. First of all your code is not ActiveX VBScript. Second, you will need to make sure the appropriate ActiveX objects your code required can be instantiated (i.e., are on the system path).
In browsers other than Microsoft Internet Explorer and in other processors, msxsl:script is not supported. .NET scripting is not supported in any browser, use ActiveX scripting instead.
On XSLT 2.0
If you can use Altova, you are essentially using XSLT 2.0, which does not require the extensions you wrote: it can natively compare times and dates, and can get the current date and time. XSLT 2.0 processors include, but are not limited to: Altova (native), Saxon (Java and IKVM.NET), Exselt (.NET), XMLPrime (.NET).
With the program BaseX I was able to use XPath and XQuery in order to query an XML document located at my home directory, but I have a problem with doing the same in XSLT.
The document I'm querying is BookstoreQ.xml.
XPath version, running totally fine:
doc("/home/ioannis/Desktop/BookstoreQ.xml")/Bookstore/Book/Title
XSLT code which I want to execute:
<xsl:stylesheet version = "2.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method= "xml" indent = "yes" omit-xml-declaration = "yes" />
<xsl:template match = "Book"></xsl:template>
</xsl:stylesheet>
I read BaseX' documentation on XSLT, but didn't manage to find a solution. How can I run given XSLT?
BaseX has no direct support for XSLT, you have to call it using XQuery functions (which is easy, though). There are two functions for doing this, one for returning XML nodes (xslt:transform(...)), one for returning text as a string (xslt:transform-text(...)). You need the second one.
xslt:transform-text(doc("/home/ioannis/Desktop/BookstoreQ.xml"),
<xsl:stylesheet version = "2.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method= "xml" indent = "yes" omit-xml-declaration = "yes" />
<xsl:template match = "Book"></xsl:template>
</xsl:stylesheet>
)
Both can either be called with the XSLT as nodes (used here), by passing it as a string or giving a path to a file containing the XSLT code.
I'm kind of new to XSLT, and I've gotten basic transformation done. Next I want to try out date manipulations, since my data will have timestamps. However, I can't seem to get any date functions to work, and it greatly frustrates me. I'm testing using Firefox 3.5, xsltproc 1.1.24, xalan 1.10, and XMLSpy 2009, and they all say that the functions I'm trying to use don't exist.
My xml looks like so:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="datetime.xsl"?>
<watcher>
<event id="1" date="2009-09-04T13:49:10-0500" type="ABCD">This is a test </event>
</watcher>
</code>
My xsl looks like so:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="event[#type='ABCD']">
<!-- Date: <xsl:value-of select="day-from-dateTime(xs:dateTime(#date))"/> -->
<!-- Date: <xsl:value-of select="day-from-dateTime(#date)"/> -->
Date: <xsl:value-of select="fn:day-from-dateTime(#date)"/>
</xsl:template>
</xsl:stylesheet>
If I make the stylesheet version 2, XMLSpy complains that it can't cast my date: XSLT 2.0 Debugging Error: Error in XPath 2.0 expression (Cast failed, invalid lexical value - xs:dateTime '2009-09-04T13:49:10-0500')
If I leave it as version 1, it complains about a different error: XSLT 1.0 Debugging Error: Error in XPath expression (Unknown function - Name and number of arguments do not match any function signature in the static context - 'day-from-dateTime')
Anytime I try to change the XSL to use a namespace, such as fn:day-from-dateTime, it refuses to work at all, with all of my parsers saying that The function number 'http://www.w3.org/2005/02/xpath-functions:day-from-dateTime' is not available and variants thereof. I know from other tests that I can use the substring() function perfectly, without needing any namespace prefix, and I believe it's in the same namespace as day-from-dateTime.
I feel like it's something incredibly easy, since all of the tutorials show functions being used, but something seems to be eluding me. Could someone show me what I'm missing?
Ouch, nasty versions thing going on here. A lot of the issues you're seeing will be because the XSLT processor you're using doesn't support XPath 2.0, which is where that day-from-dateTime function comes from.
I can get what you're trying to do to work, with a Saxon processor - Saxon-B 9.1.0.6 as my processor instead of Xalan. (Xalan appears to support XPath 1.0 only, according to the documentation)
There are a few errors in your documents:
The source document should have the timezone as 05:00, not 0500
<?xml version="1.0" encoding="UTF-8"?>
<watcher>
<event id="1" date="2009-09-04T13:49:10-05:00" type="ABCD">This is a test </event>
</watcher>
The XSLT should cast the string 2009-09-04T13:49:10-05:00 into a xs:dateTime, which is what type the argument of day-from-dateTime needs to be.
Date: <xsl:value-of select="day-from-dateTime(xs:dateTime(#date))"/>
And then it works
<?xml version="1.0" encoding="UTF-8"?>
Date: 4
Hope that helps,
I have an XML document that needs to pass text inside an element with an '&' in it.
This is called from .NET to a Web Service and comes over the wire with the correct encoding &
e.g.
T&O
I then need to use XSLT to create a transform but need to query SQL server through a SP without the encoding on the Ampersand e.g T&O would go to the DB.
(Note this all has to be done through XSLT, I do have the choice to use .NET encoding at this point)
Anyone have any idea how to do this from XSLT?
Note my XSLT knowledge isn’t the best to say the least!
Cheers
<xsl:text disable-output-escaping="yes">&<!--&--></xsl:text>
More info at: http://www.w3schools.com/xsl/el_text.asp
If you have the choice to use .NET you can convert between an HTML-encoded and regular string using (this code requires a reference to System.Web):
string htmlEncodedText = System.Web.HttpUtility.HtmlEncode("T&O");
string text = System.Web.HttpUtility.HtmlDecode(htmlEncodedText);
Update
Since you need to do this in plain XSLT you can use xsl:value-of to decode the HTML encoding:
<xsl:variable name="test">
<xsl:value-of select="'T&O'"/>
</xsl:variable>
The variable string($test) will have the value T&O. You can pass this variable as an argument to your extension function then.
Supposing your XML looks like this:
<root>T&O</root>
you can use this XSLT snippet to get the text out of it:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="root"> <!-- Select the root element... -->
<xsl:value-of select="." /> <!-- ...and extract all text from it -->
</xsl:template>
</xsl:stylesheet>
Output (from Saxon 9, that is):
T&O
The point is the <xsl:output/> element. The defauklt would be to output XML, where the ampersand would still be encoded.