使用 XSLT 从 JSON 输出中提取特定字段

Extract a specific field from JSON output using XSLT

我有一个如下所示的 xml 字段

<INPUT>{"data":"abc1234":"format":"text"}</INPUT>

我只需要从 JSON 字符串中提取数据值,在本例中为 abc1234

我期待以下输出

<OUTPUT>abc1234</OUPUT>

这里有什么帮助吗?

假设您的输入实际上包含 有效 JSON 形式为:

<INPUT>{"data":"abc1234","format":"text"}</INPUT>

你可以这样做:

<xsl:template match="INPUT">
    <OUTPUT>
        <xsl:value-of select="substring-before(substring-after(., '&quot;data&quot;:&quot;'), '&quot;')"/>
    </OUTPUT>
</xsl:template>

但是,如果输入 JSON 的格式发生变化,这将失败。

对于 XSLT 3.0,您可以使用 parse-json() 函数,然后 select data 属性 值:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
    <xsl:template match="INPUT">
        <OUTPUT><xsl:value-of select="parse-json(text())?data"/></OUTPUT>          
    </xsl:template>
</xsl:stylesheet>

如果你的数据是JSON。这个例子不是,你有 : 分隔属性,而不是 ,.