So I wanted to modify some xml.
And yes, that might mean xslt.
However, the modification wasn't nice... in Java, it was fine, but to do that in xslt... rather not.
So, I ventured into the world of 'how do you call java from xslt'.
Of course, you can.
Of course, you need to use Saxon.
Of course, then you need a PAYED version of Saxon
... Sigh. I guess I'll stick to flat xslt and a lot of work then.
Just to remember how it *can* be done:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:imro="http://www.geonovum.nl/imro/2012/1.1"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:java="http://xml.apache.org/xalan/java" exclude-result-prefixes="java"
>
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<!-- Identity template : copy all text nodes, elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="SomeXmlElement">
<xsl:copy>
<xsl:element name="newElement" >
<xsl:value-of select="java:nl.demo.MyClass.staticfunction(string(//xpathnodeselectors))"/>
</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Of course. there is a hackier way.
What you can do, is parse the xml using JSoup.
JSoup is a html parsing library... but it supports xml.
Then you can use css selectors to walk through the DOM, and modify easily.
And writing out is pretty simple...
Document document = Jsoup.parse(stream, "UTF-8", "", Parser.xmlParser());
document.outputSettings().prettyPrint(false).syntax(Document.OutputSettings.Syntax.xml);
document.selectFirst("SomeXmlElement").text(staticFunction(...);
document.toString(); // we have now modified the xml...
The sad state of java and xml, where a HTML parser makes an easier interface...
And yes, that might mean xslt.
However, the modification wasn't nice... in Java, it was fine, but to do that in xslt... rather not.
So, I ventured into the world of 'how do you call java from xslt'.
Of course, you can.
Of course, you need to use Saxon.
Of course, then you need a PAYED version of Saxon
... Sigh. I guess I'll stick to flat xslt and a lot of work then.
Just to remember how it *can* be done:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:imro="http://www.geonovum.nl/imro/2012/1.1"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:java="http://xml.apache.org/xalan/java" exclude-result-prefixes="java"
>
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<!-- Identity template : copy all text nodes, elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="SomeXmlElement">
<xsl:copy>
<xsl:element name="newElement" >
<xsl:value-of select="java:nl.demo.MyClass.staticfunction(string(//xpathnodeselectors))"/>
</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Of course. there is a hackier way.
What you can do, is parse the xml using JSoup.
JSoup is a html parsing library... but it supports xml.
Then you can use css selectors to walk through the DOM, and modify easily.
And writing out is pretty simple...
Document document = Jsoup.parse(stream, "UTF-8", "", Parser.xmlParser());
document.outputSettings().prettyPrint(false).syntax(Document.OutputSettings.Syntax.xml);
document.selectFirst("SomeXmlElement").text(staticFunction(...);
document.toString(); // we have now modified the xml...
The sad state of java and xml, where a HTML parser makes an easier interface...
Reacties
Een reactie posten