XSLT Regex Replace Function - regex

We've been pulling our hair out just trying to just trying to get a basic example of the XSLT replace function to work.
I'm leaving this text in tact for context, but you may want to skip to the update
We're using Mirth to pull in HL7 messages. We're unsure whether this supports XSLT version 2, but we believe it uses SAXON - http://saxon.sourceforge.net/, which purportedly does support XSLT2 and hence the replace function.
In any case, we tried using XSLTCake to try and get even a demo replacement to work, to no avail. We've seen this either referenced as replace or fn:replace as well as a couple other suggestions using other libraries.
If XSLT2 isn't supported by Mirth, we would need a workaround for XSLT1. We found one here: XSLT string replace - but have been unable to get this to work either.
This is a tough to get down to a single question as I'm asking alot, but here goes... Can anyone provide a working example of performing a regex replacement in an XSLT? Preferably one that will run in an online parser for reference.
Here's a sample - which apparently should work.1
Update
Thanks to Michael Kay for providing code below to determine XSLT version.
<!--Transformed using 1.0 provided by Apache Software Foundation (Xalan XSLTC)-->
So It turns out we were all wrong about Mirth using SAXON and hence supporting XSLT2. I'll update with our attempt at implementing the version 1 workaround.

First find out which XSLT processor you are using. This is straightforward: insert this
<xsl:comment>Transformed using <xsl:value-of select="system-property('xsl:version')"/> provided by <xsl:value-of select="system-property('xsl:vendor')"/></xsl:comment>
into your stylesheet to output a comment in your result document.
Once you know what programming language you are using, you can start thinking about writing code.

Related

XSLT Reports and Internet Explorer

with IE at its EOL and allowing file access from files in Chrome is not a viable option for us, what is the future of XSLT reports?
I am fairly new to this, and have just been "thrown" into finding a solution. Everything I'm finding online is years old, it's strange that no one is talking about this since "death" of IE.
our data is in XML format, using XSL templates to display formatted reports to browser via ScriptX (smsx.cab) (with page breaks, headers, etc). The user then "prints to PDF"
I am hoping to see what other organizations are doing to ensure existing XSLT reports continue to work. Converting to something else? Making them work with other, currently supported, browsers?
thank you, all and any tips, links and comments much appreciated.
You could try executing your XSLT transformations using a local script.
Take note that these solutions only support XSLT 1.0.
MSXML
successor of msxsl.exe?
PowerShell
Applying XSL to XML with PowerShell : Exception calling "Transform"
If you want to use XSLT 2.0+
You can use Saxon and call the jar file from a batch file.
https://www.saxonica.com/

Where do I get test-stylesheets to be able to use xsltproc?

I'm little lost here. I'm using googltest framework and just want to transform the xml report to an html. I found that xsltproc does the job:
xsltproc style.xsl report.xml -o report.hmtl
but first I need to get a stylesheet. Just a basic-generic stylesheet so the result can be visualized on a browser, where can I find it?
Firstly, you've tagged this XSLT 2.0, but xsltproc only supports XSLT 1.0.
Secondly, without knowing what flavour of XML is in report.xml, there isn't really very much one can do by way of a useful generic transformation to HTML. There are attempts like this one:
Generic XSLT to tabluate XML
but whether it produces results that work for you depend entirely on your input and desired output, which you haven't told us anything about.

Does SaxonCE support regex flag "!"?

I have xsl stylesheet that uses flag ! a lot. I found that very handy since xpath 2.0 specification limits regular expressions usage.
This stylesheet was intended to be used in web-based application (php) for transforming user input data and generating output, so SaxonCE came to my mind. But it throws error unrecognized flag '!' .
So, is there way around this? Does Saxon/C support ! flag at least? Or the best way is to use SaxonHE and somehow connect it with web app?
EDIT:
To be clear, ! is special flag used in Saxon products that tells xslt processor to use his own regex engine instead of xpath one, It is not w3c recommendation, more like extra feature.

Saxon, custom extension element, XTDE:unknown extension instruction

When I tried to use a custom extension element with Saxon, I got an error saying XTDE:unknown extension instruction in my XSL file. I asked this on Saxon mailing list, but haven't yet received a response, so I decided to ask here. In order to be helpful, here is the whole content from the mailing list:
from: sky
I just start using Saxon. After go through some documentations, I still found it hard to write my own custom extension instruction. I have read "writing XSLT extension instructions", and the example provided in the package net.sf.saxon.option.sql. But I'm still a little confusing: the document says,
A subclass of SimpleExpression should implement the methods getImplementationMethod() and getExpressionType(), and depending on the value returned by getImplementationMethod(), should implement one of the methods evaluateItem(), iterate(), or process().
However, there is only call() method implemented in the sql example. I'm new to XML/XSLT, hence find it hard to understand how to write my own extension elements.
Is there a tutorial of some kind which explains writing extension elements in more detail?(I have Google but found no luck, the best I can find is with older Saxon version that has different implementation). Or maybe I should go through some other XML/XSLT intermediate first?
Thanks in advance
from Michael Kay
You're right, implementing extension instructions is not easy. That's partly because the APIs are quite complex, partly because the documentation is poor, and partly because the code that would help you understand it is not open source. The underlying reason for this is that not many people have attempted to do this, so there has been little feedback that would lead to improvement over the years.
I would encourage you to ask yourself seriously whether this is something you really want to do badly enough to cope with the difficulties.
The documentation extract you cite appears to be out of date. The "Callable" interface was a relatively recent addition, and the documentation has not caught up. Implementing the call() method is enough.
Michael Kay
Saxonica
from sky
Thanks for reply.
I'm replacing Xalan with Saxon, so there are extension instructions written for Xalan that need to be rewritten. I think it would be better if I rewrite Xalan extension elements into Saxon extension functions, however, I want to give extension element a try before making the choice. Right now I have a problem with extension element. I tried to write a simple extension element, but it failed to run with "XTDE 1450: Unknown extension instruction". Here is what my code looks like:
//Config.java
ProfessionalConfiguration config = new ProfessionalConfiguration();
config.setExtensionElementNamespace("degx", "DegElementFactory");
//DegElementFactory.java
if(localname.equals("value-of")) return DegxValueOf.class;
//version.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:degx="http://DegElementFactory" extension-element-prefixes="degx">
...
<degx:value-of select="alpha 1"/>
...
</xsl:stylesheet>
I run Config.class first, and then running sf.net.saxon.Transform, I got the error above. My guess is I configured extension element namespace the wrong way. I have Saxon PE with evaluate license install correctly, because I got message about expired days after running Transform.
In summary, I have Config.class, DegElementFactory.class, DegxValueOf.class, all in my working directory, and I have add it to class path through -cp argument. Please help :)
Because I'm using net.sf.saxon.Transform from command line, the configuration class object cease to exist after the execution of Config.class. At first I thought config.setExtensionElementNamespace() would write to a configuration file somewhere. But it turns out wrong. So there are two ways to set extension element namespace:
from command line, supply -config:file argument. e.g.
net.sf.saxon.Transform -config:config.xml -s:source.xml -xsl:transform.xsl -o:result.out
invoking XSLT from application, instance a configuration class and execute setExtensionElementNamespace() method.
#Martin Honnen also pointed out another problem in comment, thanks!

How do you use Exslt.dyn (especially the 'evaluate' method) in Umbraco?

I am trying to execute the string contained in an XSL variable.
Umbraco has hooks for several Exslt pieces, but it seems the Exslt.dyn (Exslt.dynamic) is not one of them.
How do you add it in? Acceptable methods (in order of preference:
Writing your own XSLT extension (possibly using existing Umbraco code for Exslt Dynamic).
Uploading the XSL from http://www.exslt.org/dyn/functions/evaluate/index.html into Umbraco.
Modifying the Umbraco source to add it (possibly using existing Umbraco code for Exslt Dynamic).
The reason I mention Exslt Dyanmic is because some Umbraco XSLT sources show a reference that I am assuming existing in some versions of Umbraco. I cannot find it in the source code, however. (Example: the XSL sources pasted in here: http://our.umbraco.org/projects/starter-kits/business-website-starter-pack/general-%28bugs,-feedback,-feature-requests%29/8085-Changing-the-first-day-to-fx-monday)
Very few XSLT processors implement dyn:evaluate() and Umbraco obviously doesn't use one of these.
There isn't anything you can do in this case.
Ask the Umbraco developers to incorporate an XSLT 2.0 processor in the future -- XSLT 2.0 has a native <xsl:function> instruction for defining functions that can be referenced in any XPath expression.
Not sure if this will work, but if Exslt.ExsltDynamic is supported out of the box in the .Net implementation of XSLT, this should work. In your stylesheet add the namespace xmlns:Exslt.ExsltDynamic="urn:Exslt.ExsltDynamic" (as in the example you linked) and in the exclude-result-prefixes property add Exslt.ExsltDynamic to make it accessible in your xslt file.
Then you can just do something like in your template.