Doorgaan naar hoofdcontent

xml modification

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...

Reacties

Populaire posts van deze blog

Spring's conditional annotation with properties

Spring has a nice @Conditional annotation, to have the option to have beans be available in the context depending a specific condition (Of course, this can also be realized by using @Configuration objects, but that's a different post). Ideally, we'd have the option to have a condition evaluate to true or false depending on a property. Sadly, Spring does not support that out of the box. Googling and looking around gives a partial solution, but the complete one here, so we won't forget: /** * Components annotated with ConditionalOnProperty will be registered in the spring context depending on the value of a * property defined in the propertiesBeanName properties Bean. */ @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Conditional(OnPropertyCondition.class) public @interface ConditionalOnProperty { /** * The name of the property. If not found, it will evaluate to false. */ String value(); /** * if the p...

On SSL certificate generation

 So, more stuff I always forget... how to properly generate SSL certs. Well, easiest is with openssl (of course) Something like: openssl req -new -newkey rsa:2048 -nodes  -sha256 -subj "/C=NL/ST=Utrecht/L=Utrecht/O=Cooperatieve Rabobank U.A./OU=RASS Groep ICT/CN=my-common-name.host.nl" -keyout somename-prod.key -out somename-prod.csr   That can get you a certificate sign request (csr) and the appropriate key.   Of course, you want to then import those keys into a keystore. The trick to doing that is to convert it to a pkcs12 format where it can have the certificate and the key combined.   openssl pkcs12 -export -inkey somename-prod.key -in somename-prod.rabobank.nl.crt -out somename-prod.p12  Note that the crt is the signed certificate, acquired through getting the csr generated above approved..   This p12 file you can import using something like KeyStore Explorer.   After that, you can also append the root certificates of the original cert, to en...

Using spring's @transactional to only roll back when you really want to

In spring you can use the @Transactional annotation to marcate public methods as transactions. Any exception thrown in such an exception causes a rollback... Any exception? No, spring only does so on runtime exceptions. Checked exceptions are allowed and do not result in a rollback. And even then, you can allow some transactions to rollback, or not, with the proper properties for the transactions. For examle, @Transactional(rollbackFor="MyCheckedException.class") will rollback for a specific checked exception, and similarly, you can use @Transactional(noRollbackFor="MyUncheckedException.class") for unchecked exceptions. Of course, you might want to make it a little simpler on yourself. This article on stackoverflow shows us a different way: we can use our own transaction handler by overriding spring's. Let's have a sample interface which defines whether an exception should perform a rollback: interface ConfigurableRollback shouldRollbackOnE...