ish at XSLT and have a XSLT output that contains some json within elements.
I’ve been looking around to see if there is anyway for XSLT to replace the json with just a string:
Ex:
<company:Cause
xmlns:company="http://example.com/company"
xmlns:j="http://example.com/j"
xmlns:s="http://example.com/s"
xmlns:nc="http://example.com/nc"
>
<j:Cause s:id="Cause-08D628B86EF2">
<nc:DescriptionText>{"code":"PC","description":"Probable Cause"}</nc:DescriptionText>
<nc:ActivityDate>
<nc:DateTime>2018-10-02T00:00:00</nc:DateTime>
</nc:ActivityDate>
<j:Name>{"code":"PC","description":"Probable Cause"}</j:Name>
<company:Augmentation>
<j:Stat>
<j:DescriptionText>{"code":"PC","description":"Probable Cause"}</j:DescriptionText>
</j:Stat>
</company:Augmentation>
</j:Cause>
</company:Cause>
Is there anything I can add to the XSLT in order to do this on the fly? Anything already built into XSLT?
2
Answers
XSLT 3 can parse/process JSON:
Example fiddle running SaxonJS with pure JavaScript in the browser.
Example fiddle running Saxon HE 12 Java with CheerpJ 3 in the browser.
XSLT 3 has been the current version of XSLT since 2017 and is currently supported on a variety of platforms through free to use libraries like e.g. Saxon HE 12 Java for Java, SaxonC 12 for C/C++ and Python and PHP, SaxonJS 2.6 for JavaScript and Node.js and Saxon HE 10 .NET for the .NET framework or .NET 6/8.
There are also commercial offerings like Altova RaptorXML or SaxonCS.
If you’re stuck with XSLT 1.0, and you wanted to process JSON properly, tt would certainly be possible to build a full JSON parser using recursive named templates, but that may be overkill. Much simpler to do as y.arazim suggests and just strip out the "special" JSON characters by translating them all into spaces, and then use the
normalize-space()
function to convert each run of multiple spaces into a single space. e.g.NB the string of spaces which appears as the third parameter to the
translate()
function should be the same length (or longer!) than the second parameter (containing the unwanted characters). Thetranslate()
function takes the first parameter and for each character in that string it looks for that character in the second parameter, and if it’s found, it replaces it with the character at the same position in the third parameter. So the third parameter being entirely spaces means that every one of those unwanted characters will be replaced with a space.Result:
NB you will need to use the proper namespace URIs rather than the dummy values I added here.