英文:
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("http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087") as url:
data = json.load(url)
print(data)
First I tried it with doc('http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087')
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:
<?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>
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('http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087')
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('http://dme-intern.mozarteum.local/digital-editions/api/work-info/4087') => json-to-xml()
instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论