Bryan C.
Bryan C.

Reputation: 117

How to get a string from an XSL 3.0 Map

I have an XSL Map

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE stylesheet>
<xsl:stylesheet version="3.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:functx="http://www.functx.com"  
    xmlns:map="http://www.w3.org/2005/xpath-functions/map">
    
    <xsl:param name="settings" select="'https://testurl.com' ||  '/_resources/includes/settings.xml'"/>
    
    <xsl:template name="sidenav">
        <map key="style" xmlns="http://www.w3.org/2005/xpath-functions">
            <xsl:choose>
                <xsl:when test="doc-available($settings)">
                    <string key="navcss" select="doc($settings)/document/settings/ouc:div[@label='section-menu-type']"/>
                </xsl:when>
                <xsl:otherwise>
                    <string key="navcss" select="$nav-css"/>
                </xsl:otherwise>
            </xsl:choose>
        </map>
        "style" : <xsl:value-of select="map:get('style')('navcss')"/>
    </xsl:template>
    <xsl:call-template name="sidenav"/>
</xsl:stylesheet>

When I try to get the value with the below code I get: "Fatal Error: Unknown type map"

"style" : <xsl:value-of select="map:get('style')('navcss')"/>

If I substitute it with this:

<xsl:variable name="map" select="map {
    'navcss' : if(doc-available($settings)) then doc($settings)/document/settings/ouc:div[@label='section-menu-type'] else 'd3',            
}"/>
<xsl:value-of select="map:get($map, 'navcss')"/>

I get the value. My question is, can you create a map element and get its key similar to the map function, or do you just have to use the map xpath function?

Upvotes: 0

Views: 1213

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

The XML representation of JSON defined in XSLT 3.0 and XPath 3.1 uses such a map element to represent JSON objects and you can convert such XML to JSON with xml-to-json and feed it to parse-json; on the other hand to create a map in XSLT 3.0 I would suggest to use the xsl:map and xsl:map-entry elements or to simply use XPath 3.1 expressions:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="#all"
    expand-text="yes"
    version="3.0">
    
  <xsl:param name="json-xml">
      <map xmlns="http://www.w3.org/2005/xpath-functions">
          <string key="foo">bar</string>
          <number key="pi">3.1415927</number>
      </map>
  </xsl:param>
  
  <xsl:variable name="map1" select="xml-to-json($json-xml) => parse-json()"/>
  
  <xsl:param name="map2" as="map(*)">
      <xsl:map>
          <xsl:map-entry key="'foo'" select="'bar'"/>
          <xsl:map-entry key="'pi'" select="math:pi()"/>
      </xsl:map>
  </xsl:param>
  
  <xsl:template match="root">
      <section>
          <h2>Example</h2>
          <p>{$map1?pi}</p>
          <p>{$map1?foo}</p>
      </section>
      <section>
          <h2>Example</h2>
          <p>{$map2?pi}</p>
          <p>{$map2?foo}</p>
      </section>
  </xsl:template>

As for your expression in the comment, it seems you could just use e.g.

select="if (doc-available($settings)) 
        then map { 'navcss' : doc($settings)/document/settings/ouc:div[@label='navcss'], 'navtype' :  doc($settings)/document/settings/ouc:div[@label='navtype']}
        else map { 'navcss' : 'd3', 'navtype' : 'sectionnav' }"

I haven't spelled out all map properties but I hope it is clear how to list further properties there.

Upvotes: 1

Related Questions