在 XSLT 中针对文字双引号测试节点

Testing nodes against literal double quote in XSLT

我有一个来源 XML 看起来像这样:

<root>
    <item>a</item>
    <item>b</item>
    <item>"</item>
</root>

我正试图将其转换成一个 JSON 对象,大致如下所示:

{"elements": [
    {"value": "a"},
    {"value": "b"},
    {"value": "\""}
]}

我几乎可以使用一个大致如下所示的 XSLT 文件:

<xsl:template match="root">
    <xsl:text>{"elements":[</xsl:text>
        <xsl:apply-templates select="item"/>
    <xsl:text>]}</xsl:text>
</xsl:template>

<xsl:template match="item">
    <xsl:text>{</xsl:text>
        <xsl:text>"value":"</xsl:text>
            <xsl:choose>
                <xsl:when test="current()=&quot;]">
                    <xsl:text>\"</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="current()"/>
                </xsl:otherwise>
            </xsl:choose>
        <xsl:text>"</xsl:text>
    <xsl:text>}</xsl:text>
    <xsl:if test="position() != last()">
        <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:template>

问题是,当我 运行 这样做时,我得到以下回溯:

Traceback (most recent call last):
  File "run_test.py", line 26, in <module>
    single_test("test.xml", "test.xslt")
  File "run_test.py", line 7, in single_test
    transform = etree.XSLT(xslt_doc)
  File "src/lxml/xslt.pxi", line 410, in lxml.etree.XSLT.__init__
lxml.etree.XSLTParseError: xsl:when : could not compile test expression 'current()="]'

现在我已经尝试以几种不同的方式访问当前节点的文本值,其中许多方式在初始写入时成功,但 none 我已设法将其中的方式合并到我的测试中。这是我试过的:

所有这些都给了我同样的错误,这告诉我我一定是误会了。关于如何进行这种比较有什么建议吗?首选 XSLT 1.0。

XPath 表达式中的文字字符串必须用引号引起来。而不是:

<xsl:when test="current()=&quot;]">

(也有多余的右括号)使用:

<xsl:when test="current()='&quot;'">

或很快:

<xsl:when test=".='&quot;'">