xsl-list
[Top] [All Lists]

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

2019-03-11 02:29:30
Am 11.03.2019 um 08:22 schrieb Mukul Gandhi 
gandhi(_dot_)mukul(_at_)gmail(_dot_)com:
Hi Martin,
   Thanks for the answer.

Deriving from your XSLT stylesheet, I wrote following that works,

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                         xmlns="http://www.w3.org/2005/xpath-functions";
                         version="3.0">
    <xsl:output method="text"/>
    <xsl:template match="root">
        <map>
            <map key="{local-name()}">
                <array key="val">
                    <xsl:apply-templates/>
                </array>
            </map>
        </map>
    </xsl:template>
    <xsl:template match="val">
        <!--<number>{.}</number>-->
        <number><xsl:value-of select="."/></number>
    </xsl:template>
    <xsl:template match="/">
        <xsl:variable name="json-xml">
            <xsl:apply-templates/>
        </xsl:variable>
        <xsl:value-of select="xml-to-json($json-xml, map { 'indent' : true() })"/>
    </xsl:template>
</xsl:stylesheet>

If I write, <number>{.}</number> in above stylesheet, as you've suggested instead of mine <number><xsl:value-of select="."/></number>, I get following error

xml-to-json: Invalid number: {.}
(produced by Saxon PE)

Any idea, why this is so?


Well, my suggestion tried to make use of the XSLT 3 feature of text value templates with the attribute expand-text="yes"


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

        xmlns:xs="http://www.w3.org/2001/XMLSchema";
    <http://www.w3.org/2001/XMLSchema>
        exclude-result-prefixes="#all"
        xmlns="http://www.w3.org/2005/xpath-functions";
    <http://www.w3.org/2005/xpath-functions>
        expand-text="yes"


declared above

        version="3.0">

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

      <xsl:output method="text"/>
      <xsl:strip-space elements="*"/>

      <xsl:template match="root">
          <map>
              <map key="{local-name()}">
                  <array key="val">
                      <xsl:apply-templates/>
                  </array>
              </map>
          </map>
      </xsl:template>

      <xsl:template match="val">
          <number>{.}</number>


so that here the {.} is evaluated and not taken literally.

--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>