[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: is XPath 3.1 xml-to-json() function useful

Subject: Re: is XPath 3.1 xml-to-json() function useful
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 8 Mar 2019 22:38:28 -0000
Re:  is XPath 3.1 xml-to-json() function useful
On 08.03.2019 23:17, Martin Honnen martin.honnen@xxxxxx wrote:
On 08.03.2019 23:07, Willem Van Lishout willemvanlishout@xxxxxxxxx wrote:
I've been experimenting with the xml-to-json function, but for some reason my output is escaped. Why is that?


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
B B B B xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:f="http://www.w3.org/2005/xpath-functions" version="3.0">
B B B B <xsl:output encoding="UTF-8" method="json"/>
B B B B <xsl:template match="/">
B B B B B B B B <xsl:variable name="transformed">
B B B B B B B B B B B B <xsl:apply-templates select="programs"/>
B B B B B B B B </xsl:variable>
B B B B B B B B <xsl:value-of select="xml-to-json($transformed, map{'indent': true()})"/>

The function returns a string you can output as such with the xsl:output method="text", you have used "json" instead which then applies JSON escaping rules on the string you have.

You can use the "json" output method if your result is an XPath 3.1 value like an array or a map that can be serialized as JSON:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<xsl:mode on-no-match="shallow-skip"/>

<xsl:output method="json" indent="yes"/>

  <xsl:function name="mf:apply-templates" as="item()*">
      <xsl:param name="input" as="item()*"/>
      <xsl:apply-templates select="$input"/>

  <xsl:template match="programs">
      <xsl:sequence select="array { mf:apply-templates(*) }"/>

  <xsl:template match="program">
      <xsl:sequence select="map { 'title' : string(@title) }"/>



Unfortunately XSLT 3 lacks an xsl:array instruction so I had to introduce that function on the XPath level to construct the array items.

If you use Saxon PE or EE you can use saxon:array instead.

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.