Saxon, custom extension element, XTDE:unknown extension instruction - xslt

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!

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/

XSLT Regex Replace Function

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.

How to parse HTML with C++/Qt?

How can i parse the following HTML
<body>
<span style="font-size:11px">12345</span>
<a>Hello<a>
</body>
I would like to retrive the data "12345" from a "span" with style="font-size:11px" from www.testtest.com, but I only want the that very data, and nothing else.
How can I accomplish this?
I think QXmlQuery is what you want.
I think the code will be like
QXmlQuery query;
query.setQuery(html, QUrl("/body/span[#style='font-size:11p']"));
QString r;
query.evaluateTo(&r);
You can also provide URL directly to the query
query.setQuery(QUrl("http://WWW.testtest.com"), QUrl("/body/span[#style='font-size:11p']"));
EDIT: From the Qt 5.6 release blog post:
With 5.6, Qt WebKit and Qt Quick 1 will no longer be supported and are dropped from the release. The source code for these modules will still be available.
So, as of Qt 5.6 – unless you're willing to compile the sources –, QtWebKit is no longer available. If you're using a Qt release older than 5.6 ot are willing to compile QtWebKit, this might be helpful; otherwise this answer is no longer valid.
It is hard to tell you exactly what needs to be done as your explanation is incomplete about the use case. However, there are two ways of proceeding.
QtWebKit
If you already need any other functionality from that module, this is not going to introduce any further dependencies, and it will be the most convenient for you to use.
You need to get the https://doc.qt.io/archives/qt-5.5/qwebelement.html
That will come once you find the first "span" element in your html:
https://doc.qt.io/archives/qt-5.5/qwebframe.html#findFirstElement
Then, you can simply get the text for that element with the corresponding QWebElement method(s). For instances, you can use this one for getting an attribute value:
https://doc.qt.io/archives/qt-5.5/qwebelement.html#attribute
... but you can also request the attribute names as you can see in the documentation, etc.
This is how you will get the 12345 value:
https://doc.qt.io/archives/qt-5.5/qwebelement.html#toPlainText
XML parser in QtCore
If you do not need webkit for your sotware, and the html data comes in a different way rather than directly from the web for which you would need to use QWebKit, then you are better off using the xml parser available in QtCore. It still might be the case even if you do not have any other dependency from QtWebKit that this additional dependency will not cause any issues in your use case. It is hard to tell based upon your description. For sure, this would be less convenient, albeit not that much, compared to the webkit based solution as that is designed for html.
What you need to avoid is QtXmlPatterns. It is an unmaintained software as of now, and that would introduce an additional dependency for your code either way.

Design approaches to read/write different version of same config file

In our project we got an application that uses an external configuration file (say server.xml). Now we need to design a setup tool GUI in C++/QT to read/edit such configuration file and it should be able to handle all the different versions of such file. The user will choose the file version and then proceed with the editing. From one version to another doesn't change too much, maybe there is a new xml tag, a tag with a different name or in a different position.
What's the best design approach to do so? We are planning to go for a standard MVC design pattern but how to deal with all the different configuration versions without rewriting the same GUI code again n again?
Here the sample config file:
<?xml version="1.0" encoding="utf-8"?>
<Server_configuration ver="11">
<core>
<enable-tms>true</enable-tms>
<enable-gui-messages>true</enable-gui-messages>
<waiting-for-config-timeout>10000</waiting-for-config-timeout>
<remoting>
<port>50000</port>
<join-timeout>5000</join-timeout>
<ismultithread>true</ismultithread>
<maxconcurrentrequests>20</maxconcurrentrequests>
</remoting>
</core>
<content>
<ftp>
<ip>192.168.0.227</ip>
<port>21</port>
<userid>******</userid>
<passwd>******</passwd>
</ftp>
<library>
<ip>192.168.0.227</ip>
<port>50023</port>
</library>
<local>
<asset-root>/assetroot</asset-root>
<kdm-expiration-warning>172800000</kdm-expiration-warning>
</local>
<hula-store-daemon>
<ip>127.0.0.1</ip>
<port>5567</port>
</hula-store-daemon>
</content>
</Server_configuration>
This is no means a drop in solution but I here are some things to do/consider. Every situation will differ.
Have an explicit version identifier in your config files. Fingerprinting them is a real (error prone) pain.
Consider having a tool that will update from version to version. It will be easier than reading old versions and trying to apply them.
I may be easier to do every version step individually but this can make the conversions less "lossless". A happy hybrid is to do minor updates from version to version but have "checkpoint" major upgrades that will jump right to the latest (or the latest "checkpoint"). This is kinda like incremental backups with full backup snapshots every once and a while.
Keep the user informed. A sysadmin won't be happy if you are changing his settings. You might want to make the process interactive or put comments into the file of every added/moved/removed setting. I would also recommend keeping removed settings in some section of the file for user reference. (Put a note why they are there as well).
Backup the old file. Your script will crash and it will eat data. Do something like naming the current file ${oldname}.old-${ver}~. Saving the settings in a different section of the file won't always be enough and this will save your users a lot of heartache.
Versioning should always be designed as robust and as simple as possible. It is crucial for you to determine whether each version of your application must be compatible with each version of the setup tool (which is rare), or whether you can, for example, meet your needs if any newer setup tool works with any same or older application, but not vice versa.
One way compatibility
One possibility to design for the latter is to add a version attribute to the XML file but try to keep it at the same fixed value forever by always only changing the structure and semantics of the XML file in backward compatible ways. For example, adding an element is backward compatible as long as the setup tool can interpret its absence the same way both the old setup tool and the application would behave. It does not hurt that the new setup tool always writes an (equivalent) value to the new element, because two-way compatibility with the old application is not required.
Once the day comes when you cannot maintain backward compatibility on input, you just change the value of the version attribute and start special casing it in the setup tool.
If you validate the XML against an XSD, notice that XSD can actually do one frequently useful thing for you: assign default attribute values. This way, your setup tool's source code may not even actually notice that the underlying document was missing a recently added attribute!
Two way compatibility
Strict versioning is needed. A schema definition (XSD, RelayNG,...) should be defined for each version of the XML file and the file should be validated against it both when it is read by the setup tool, written by the setup tool, or read by the application. The schema definition may be identical for several consecutive versions, if the interpretation of the same XML has changed, so when in doubt, always increase the version number.
Do what you can educating everyone that they cannot just edit the latest schema and do away with that. Unreliable versioning is worse than no versioning.

building objects from xml file at runtime and initializing, in one pass?

I have to parse the XML file and build objects representation based on that, now once I get all these data I create entries in various database for these data objects. I have to do second pass over that for value as in the first pass all I could do is build the assets in various databases. and in second pass I get the values for all the data and put it in the database.
I have a feeling that this can be done in a single pass but I just want to see what are your opinions. As I am just a student who started with professional work, experienced ppl please help.
Can someone who have ideas or done similar work, please provide some light on the topic so that I can think over the possibility of the work and get the prototype going based on your suggestion.
Thanks a lot for your precious time, I honestly appreciate it.
You might be interested in learning several techniques of building XML parsers like DOM or SAX. As it is said in SAX description the only thing which requires second pass could be the XML validation but not the creating the tree.
Beside DOM and SAX parsing, you can use XQuery for querying data from XML files.It is fast, robust and efficient.
here is a link
You can use Qt Xml module for DOM ,SAX and XQuery, btw it is open source.
Another option is xml - C++ data binding, Here is the link.You can create C++ codes from definition directly.It is an elegant solution.
EDIT:
the latter one is at compile time.
You can also use Apache Licensed http://xmlbeansxx.touk.pl/. It works under Windows and Linux.
you could take a look at the somewhat simpler 'pull' api called stax instead of using sax (event based).