如何从XSL 3.0映射中获取字符串



我有一个XSL映射

<?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>

当我试图用下面的代码获得值时,我得到:";致命错误:未知类型映射";

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

如果我用这个来代替它:

<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')"/>

我得到了价值。我的问题是,你能创建一个map元素并获得类似于map函数的键吗?还是只需要使用map-xpath函数?

XSLT3.0和XPath3.1中定义的JSON的XML表示使用这样的map元素来表示JSON对象,您可以使用xml-to-json将这样的XML转换为JSON并将其提供给parse-json;另一方面,为了在XSLT3.0中创建映射,我建议使用xsl:mapxsl:map-entry元素,或者简单地使用XPath3.1表达式:

<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>

至于你在评论中的表达,你似乎可以使用例如

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' }"

我还没有详细说明所有的地图属性,但我希望能清楚地列出更多的属性。

最新更新