How to decode base64 string as HTML content in XSLT? - html

I'm trying to convert my encoded base64 HTML content from an attribute and place it in the HTML content using XLST, hope somebody can help.
I have this string:
<p><b>Hello!</b><span>This is a span</span></p>
And this is the base64 encoded value:
PHA+PGI+SGVsbG8hPC9iPjxzcGFuPlRoaXMgaXMgYSBzcGFuPC9zcGFuPjwvcD4=
The XML data looks like this:
<Data Get="True">
<Result TextFlow="PHA+PGI+SGVsbG8hPC9iPjxzcGFuPlRoaXMgaXMgYSBzcGFuPC9zcGFuPjwvcD4=">
</Result>
</Data>
My last attempt was using this:
fn:cast($XML/Data/Result/#TextFlow,'xs:base64Binary','xs:String',false())
That code send this error:
Unknown function - Name and number or arguments do not match any function signature in the static context.
Thanks for your help!

There's no built-in capability in XSLT to do this.
Coming soon is the EXPath binary module:
http://expath.org/spec/binary
The bin:decode-string() function is what you need. We've got an implementation of this for Saxon but it's not integrated into the product yet. Meanwhile there is the extension function saxon:base64BinaryToString which is essentially the same:
http://www.saxonica.com/documentation/index.html#!functions/saxon/base64Binary-to-string
and which is available in Saxon-PE or higher.
Both functions take an argument of type xs:base64Binary; to convert a string in base64 to a value of this type, you use the constructor function xs:base64Binary(string). This requires XSLT 2.0 of course.
If your (decoded) string contains markup, then to copy it to the serialized HTML output you will need to use the deprecated disable-output-escaping="yes" option.
UPDATE (April 2019)
Recent releases of Saxon (the current release is 9.9) include the EXPath binary module as standard in Saxon-PE and higher editions.

Related

load rdf/json from URL using DotNetRDF

I'm new to the World of triplets :-) I'm trying to use DotNetRDF to load the SOLR searchresult into a Graph using DotNetRDF.
The URL I'm getting data from is:
https://nvv.entryscape.net/store/search?type=solr&query=rdfType:https%5C%3A%2F%2Fnvv.entryscape.net%2Fns%2FDocument+AND+context:https%5C%3A%2F%2Fnvv.entryscape.net%2Fstore%2F1
The format is supposed to be "RDF/JSON". No matter what parser or what I try - I only get "invalid URI". Have tried to load from the URL and also tried downloadning the result to a file and load from file, same error.
I'm using VS2017 and have "nugetted" the latest version of DotNetRdf.
Please help me, what am I missing?
Regards,
Lars Siden
It looks like the JSON being returned by that endpoint is not valid RDF/JSON. It does appear to contain some RDF/JSON fragments but they are wrapped up inside another JSON structure. The RDFJSONParser in dotNetRDF requires that your entire JSON document be a single, valid chunk of RDF/JSON.
The value at resource.children[*].metadata is an RDF/JSON object. So is the value at resource.children[*].info. The rest is wrapper using property names that are not valid IRIs (hence the parser error message).
Unfortunately there is no easy way to skip over the rest of the JSON document and only parse the valid bits. To do that you will need to load the JSON document using Newtonsoft.JSON and then serialize each valid RDF/JSON object you are interested in as a string and load that using the RDFJSONParser's Load(IGraph, TextReader) or Parse(IRdfHandler, TextReader) method.

How to omit fields when using camel-xmljson JAR to convert XML string to JSON string

XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
xmlJsonFormat.setEncoding("UTF-8");
xmlJsonFormat.setForceTopLevelObject(true);
xmlJsonFormat.setTrimSpaces(true);
xmlJsonFormat.setSkipNamespaces(true);
xmlJsonFormat.setRemoveNamespacePrefixes(true);
Pool a XML file from source directory and transform it to JSON format before sending it to destination directory
from("file:/inn?noop=true").marshal(xmlJsonFormat)
.setHeader(Exchange.FILE_NAME, simple("${file:onlyname.noext}.json"))
.to("file:/out").end();
It works great when i want a JSON string right from a XML without using a POJO.
But now consider a XML where I would like to omit an element.
Should I use another lib but camel-xmljson?
Is this way the right way of accomplish it:
JAXB lib to get a POJO from XML
Jackson lib to get a JSON from POJO and with excluding POJO fields from marshalling
I understand, even I would try to avoid any POJO's in camel routes.
Having said that, I wont just straight way let the XML out. I would put a XSLT after the xmljson component, so that I can trim the xml for desired output.
Thanks gnanagurus! This is quite simple using XSLT. Moreover the xslt: component is included in the last version of the core of Camel.
I defined an xlst file 'transform.xsl' taht includes the field that is to be omited. And then defined the Camel route like that:
from("file:/inn?noop=true")
.to("xslt:file:src/transform.xsl")
.marshal(xmlJsonFormat)
.setHeader(Exchange.FILE_NAME, simple("${file:onlyname.noext}.json"))
.to("file:/out").end();

Delphi Datasnap - How to customize JSON marshalling

In my Delphi 10.1 Berlin Datasnap REST application, I need to customize the JSON serializaton of an object.
I would like to find a solution that makes use of the JSONReflect attribute, and doesn't involve the creation of Converters and Reverters for every specific field, as described in this article by Daniele Teti.
In particular, I'm trying to serialize an object that contains:
a binary file, to convert in JSON representation - like a byte array
some TDateTime fields, to convert in a String with ISO format
I have found a technical PDF document by Marco Cantù, that talks about JSONReflect attribute to enable conversion of fields, but I cannot find documentation about it.
Anyone can help me, please?
Use of JSONReflect attribute automatically implies use of converters and reverters. Delphi XE6 ships with sample project MarshallUnmarshall where JSON serialization is covered. RAD Studio Demo Code is also available online.
If you want to serialize an object that contains a TDateTime field in a string with ISO format, you can also use standard Tjson class defined in Rest.Json unit. It contains an ObjectToJsonString method. In the AOptions parameter you can specify to format dates using ISO standard.
class function ObjectToJsonString(AObject: TObject; AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]): string;
To serialize a binary file in JSON Daniele Teti has sample code in his Delphi Cookbook. Unfortunately I can not share sample code I think. Recommended reading! Second edition has just appeared.

String XML to JSON conversion in .NET without using XmlDocument

I'm trying to find a more memory efficient solution for converting XML string to JSON string (and vice versa)
without using XmlDocument.
Currently, all 3rd party libraries i tried, expects XmlDocument as input.
Before I'm writing my own parser using XmlReader, i was wondering if anyone know of a out of the box solution?
What are you trying to do exactly: Generate JSON directly from XML or deserialize the XML string to an object and then serialize it to JSON?
If you need a XmlSerializer take a look into this one I created (it uses XmlReader internally), you can find the code and how to use it here:
XML serialization using Generics
I ended up writing my own thin LightXmlDocument which holds a tree of objects representing xml elements.
LoadXml method implemented using XmlReader, i'm reading the xml string and building the tree.
Tested with 10 threads each thread iterating 900 times over different xml sizes:

SAXParseException: Element type SOAP:Text must be followed by either attribute specifications, ">" or "/>"

I'm attempting to read a response from a web service call in a junit test running in Eclipse Galileo. I'm able to successfully receive responses except when the response is a SOAP fault. Then I get the following exception:
org.xml.sax.SAXParseException: Element type "SOAP:Text" must be followed by either attribute specifications, ">" or "/>"
I have validated the XML in LiquidXML Studio against the SOAP 1.2 schema and it checks out.
Here is the XML response that SAX appears to be choking on. It has been stripped to the minimum in an attempt to eliminate anything obvious (I even made sure it didn't have any self closing elements):
<SOAP:Envelope xmlns:SOAP="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP_ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP:Header>
</SOAP:Header>
<SOAP:Body>
<SOAP:Fault>
<SOAP:Code>
<SOAP:Value>SOAP:Sender</SOAP:Value>
<SOAP:Subcode>
<SOAP:Value>SOAP:Sender</SOAP:Value>
</SOAP:Subcode>
</SOAP:Code>
<SOAP:Reason>
<SOAP:Text xml:lang="">
</SOAP:Text>
</SOAP:Reason>
<SOAP:Node>
</SOAP:Node>
<SOAP:Role>
</SOAP:Role>
<SOAP:Detail>
</SOAP:Detail>
</SOAP:Fault>
</SOAP:Body>
</SOAP:Envelope>
Any help would be appreciated.
Its obviously not regcognising 'xml:lang=""' as an attribute. CHeck with your xsd or xmlSchema what attributes are valid. Also you should be using
xml:lang=""
rather than "" although most parsers forgive you for this.
I think the problem is in mapping the soap fault xml to its corresponding object.
It turns out the problem was related to a tool I was using to return static string responses to web service requests. The static response XML contained the xml:lang attribute. However, when the tool was returning the static string, it was modifying it on the way out and replacing xml:lang on-the-fly with the fully qualified namespace equivalent {http://www.w3.org/XML/1998/namespace}lang. When this response was received, the SAXParser was choking because it couldn't interpret the fully qualified equivalent.
The tool returning the static responses used a Groovy xml parser as an integral part of sending the response.
The XmlParser Groovy class has a constructor that I had to change to set validating and namespaceAware attributes to false. So instead of XmlParser(), the tool now calls XmlParser(false, false).
Problem solved.
Thanks for the responses.