从API中提取JSON,使用XSLT

huangapple go评论107阅读模式
英文:

Fetch JSON from API using XSLT

问题

我需要从API中使用XSLT 3获取一个JSON文件,类似于Python中的以下代码:

import urllib.request, json 
with urllib.request.urlopen("http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087") as url:
    data = json.load(url)
    print(data)

首先,我尝试使用doc('http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087'),但由于该函数期望一个XML文档,我收到以下错误消息:

无法在前导部分中包含内容。

另外,似乎这个扩展dp:url-open会产生所需的结果(参见此帖子)。但是,当我使用Saxon EE 11.4调用XSLT转换时,我收到以下错误消息:

未知的扩展指令

我想扩展应该在Saxon中进行某种声明(?)。我在Saxon文档中进行了搜索,类似于这个部分,但没有找到我需要的信息。

以下是我的测试XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<stylesheet exclude-result-prefixes="xs xd dme functx dp" version="3.0"
  xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp"
  xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:dme="http://www.mozarteum.at/ns/dme"
  xmlns:functx="http://www.functx.com" xmlns:map="http://www.w3.org/2005/xpath-functions/map"
  xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" xmlns:xi="http://www.w3.org/2001/XInclude"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xpath-default-namespace="http://www.music-encoding.org/ns/mei">
  
<template match="/">
  <variable name="test">
    <dp:url-open target="http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087"/>
  </variable>
  <copy-of select="$test"/>
</template>

</stylesheet>

附言:我使用的是oXygen XML编辑器 25.0,构建版本为2023013006。

英文:

I need to fetch a JSON file from an API using XSLT 3, something like this would be in python:

    import urllib.request, json 
    with urllib.request.urlopen(&quot;http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087&quot;) as url:
        data = json.load(url)
        print(data)

First I tried it with doc(&#39;http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087&#39;) but as the function expects an XML document I'm getting this error:

> Content is not allowed in prolog.

Otherwise it seems that this extension dp:url-open would yield the needed result (cf. this post). However, when invoking an XSLT transformation using Saxon EE 11.4 I'm getting this error:

> Unknown extension instruction

I suppose that the extension should be somehow declared for Saxon (?)
I've searched in the Saxon documentation like this section
but did not find what I need.

Here is my test XSLT:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;stylesheet exclude-result-prefixes=&quot;xs xd dme functx dp&quot; version=&quot;3.0&quot;
      xmlns:dp=&quot;http://www.datapower.com/extensions&quot; extension-element-prefixes=&quot;dp&quot;
      xmlns=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:dme=&quot;http://www.mozarteum.at/ns/dme&quot;
      xmlns:functx=&quot;http://www.functx.com&quot; xmlns:map=&quot;http://www.w3.org/2005/xpath-functions/map&quot;
      xmlns:xd=&quot;http://www.oxygenxml.com/ns/doc/xsl&quot; xmlns:xi=&quot;http://www.w3.org/2001/XInclude&quot;
      xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
      xpath-default-namespace=&quot;http://www.music-encoding.org/ns/mei&quot;&gt;
    
   &lt;template match=&quot;/&quot;&gt;
      &lt;variable name=&quot;test&quot;&gt;
        &lt;dp:url-open target=&quot;http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087&quot;/&gt;
      &lt;/variable&gt;
    &lt;copy-of select=&quot;$test&quot;/&gt;
  &lt;/template&gt;
    
    &lt;/stylesheet&gt;

P.S. I'm invoking the transformation using oXygen XML editor 25.0, build 2023013006.

答案1

得分: 1

如果您从该API获取JSON,您可以在XSLT代码中使用json-doc('http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087')来获得XPath 3.1/XSLT 3.0 XDM映射或数组。要选择/查找XDM映射/数组中的数据,请参阅https://www.altova.com/training/xpath3/xpath-31#new-xpath-3.1-operators或XPath 3.1规范中有关数组和映射以及使用查找运算符的类似部分https://www.w3.org/TR/xpath-31/#id-lookup。

如果您想要JSON的XML表示,请改用unparsed-text('http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087') => json-to-xml()

英文:

If you get JSON from that API you can use json-doc(&#39;http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087&#39;) to have an XPath 3.1/XSLT 3.0 XDM map or array in your XSLT code. To select/find data in XDM maps/arrays see https://www.altova.com/training/xpath3/xpath-31#new-xpath-3.1-operators or the similar sections in the XPath 3.1 spec on arrays and maps and using the lookup operator https://www.w3.org/TR/xpath-31/#id-lookup.

If you want an XML representation of the JSON use unparsed-text(&#39;http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087&#39;) =&gt; json-to-xml() instead.

huangapple
  • 本文由 发表于 2023年3月7日 18:42:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定