使用 Saxon-HE 在 XSLT 2.0 中检索 hashmap 值 [英] Retrieving hashmap values in XSLT 2.0 Using Saxon-HE

查看:109
本文介绍了使用 Saxon-HE 在 XSLT 2.0 中检索 hashmap 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 XSLT 2.0 版本中将 Map 对象作为参数传递,并且我想使用 Saxon-HE 在 XSLT 2.0 文件下检索 Map 对象数据.

我在谷歌上搜索了很多,发现

解决方案

至于在 XSLT 中读取 XDM 映射,请参见 https://xsltfiddle.liberty-development.net/6qVRKwZ 其中有三个例子

<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:array="http://www.w3.org/2005/xpath-functions/array"exclude-result-prefixes="#all"展开文本=是"版本=3.0"><xsl:param name="mapData" as="map(xs:string, xs:string)" select="map { '1' : '188 E 6th Street' }"/><xsl:mode on-no-match="shallow-copy"/><xsl:output method="html" indent="yes" html-version="5"/><xsl:template match="/"><头><title>.NET XSLT 小提琴示例</title><身体><h1>XPath 3.1 地图示例</h1><部分><h2>函数调用语法</h2><p><code>$mapData('1')</code>:<code>{$mapData('1')}</code></p></节><部分><h2>map:get</h2><p><code>map:get($mapData, '1')</code>: <code>{map:get($mapData, '1')}</code></></节><部分><h2><code>?</code>操作员<p><code>$mapData?('1')</code>:<code>{$mapData?('1')}</code></p></节></html></xsl:模板></xsl:stylesheet>

要从 Java 代码设置此参数,请考虑使用 Saxon s9api http://saxonica.com/html/documentation9.8/using-xsl/embedding/s9api-transformation.html 运行 Saxon 然后你可以使用 hhttp://saxonica.com/html/documentation9.8/javadoc/net/sf/saxon/s9api/Xslt30Transformer.html#setStylesheetParameters-java.util.Map- 传入一个 XdmMap,该 XdmMap 是从您的 Java Map 构建的,带有 http://saxonica.com/html/documentation9.8/javadoc/net/sf/saxon/s9api/XdmMap.html#makeMap-java.util.Map-.

简短的 Java 示例是

public static void MapExample() 抛出 SaxonApiException {处理器处理器=新处理器(假);XsltExecutable 可执行文件 = processor.newXsltCompiler().compile(new StreamSource("sheet.xsl"));Xslt30Transformer 转换器 = executable.load30();映射<字符串,字符串>mapData = new HashMap();mapData.put("1", "188 E 6th Street");HashMap参数 = new HashMap<>();parameters.put(new QName("mapData"), XdmMap.makeMap(mapData));转换器.setStylesheetParameters(参数);转换器.应用模板(新流源(input1.xml"),transformer.newSerializer(System.out));System.out.println();}

I want to pass Map object as parameter in XSLT 2.0 version and i want to retrieve the Map object data under XSLT 2.0 file using Saxon-HE.

I googled a lot and found Retrieving hashmap values in XSLT

link which completely matches according to my need but i am getting exception such as

Static error in {map:get($mapData,'1')} in expression in xsl:variable/@select on line 23 column 94 of transformer.xslt: XPST0017: Cannot find a 2-argument function named {http://ns.saxonica.com/map}get().

I don't know where i am doing mistake.

These are the my files. SexsonDemo.java

   public static void transform(String xmlFile, String xslFile) throws TransformerException,
      TransformerConfigurationException {

   TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer(new StreamSource(new File(xslFile)));
   Map<String,String> mapData = new HashMap<String,String>();
   mapData.put("1", "188 E 6th Street");
   transformer.setParameter("mapData", mapData);
   transformer.transform(new StreamSource(new File(xmlFile)), new StreamResult(System.out));
  }


transformer.xsl

xmlns:map="http://ns.saxonica.com/map" exclude-result-prefixes="map" >

<xsl:variable name="mapData"/>
<xsl:variable name="addressData" select="map:get($mapData,'1')"/>

at below line i getting exception map:get($mapData,'1')

解决方案

As for reading XDM maps in XSLT, see https://xsltfiddle.liberty-development.net/6qVRKwZ which has three examples

<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:array="http://www.w3.org/2005/xpath-functions/array"
    exclude-result-prefixes="#all"
    expand-text="yes"
    version="3.0">

  <xsl:param name="mapData" as="map(xs:string, xs:string)" select="map { '1' : '188 E 6th Street' }"/>

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

  <xsl:output method="html" indent="yes" html-version="5"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>.NET XSLT Fiddle Example</title>
      </head>
      <body>
          <h1>XPath 3.1 map example</h1>
          <section>
              <h2>function call syntax</h2>
              <p><code>$mapData('1')</code>: <code>{$mapData('1')}</code></p>
          </section>
          <section>
              <h2>map:get</h2>
              <p><code>map:get($mapData, '1')</code>: <code>{map:get($mapData, '1')}</code></p>
          </section>
          <section>
              <h2><code>?</code> operator</h2>
              <p><code>$mapData?('1')</code>: <code>{$mapData?('1')}</code></p>
          </section>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

For setting this parameter from your Java code, consider using the Saxon s9api http://saxonica.com/html/documentation9.8/using-xsl/embedding/s9api-transformation.html to run Saxon and then you can use hhttp://saxonica.com/html/documentation9.8/javadoc/net/sf/saxon/s9api/Xslt30Transformer.html#setStylesheetParameters-java.util.Map- to pass in an XdmMap constructed from your Java Map with http://saxonica.com/html/documentation9.8/javadoc/net/sf/saxon/s9api/XdmMap.html#makeMap-java.util.Map-.

Short Java sample is

public static void MapExample() throws SaxonApiException {
    Processor processor = new Processor(false);

    XsltExecutable executable = processor.newXsltCompiler().compile(new StreamSource("sheet.xsl"));

    Xslt30Transformer transformer = executable.load30();

    Map<String,String> mapData = new HashMap<String,String>();
    mapData.put("1", "188 E 6th Street");

    HashMap<QName, XdmValue> parameters = new HashMap<>();

    parameters.put(new QName("mapData"), XdmMap.makeMap(mapData));

    transformer.setStylesheetParameters(parameters);

    transformer.applyTemplates(new StreamSource("input1.xml"), transformer.newSerializer(System.out));

    System.out.println();        
}

这篇关于使用 Saxon-HE 在 XSLT 2.0 中检索 hashmap 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆