如何打印xml字典值中的所有键

如何打印xml字典值中的所有键,xml,xslt,Xml,Xslt,我有这个xml <namedAnchor> {'Beacon': 'ORG', 'One': 'CARDINAL', 'Meadows': 'ORG', 'Congress': 'ORG', 'end of the month': 'DATE', 'second': 'ORDINAL', 'Tuesday': 'DATE', 'Wednesday': 'DATE', 'third': 'ORDINAL', 'New Yorker': 'NORP', 'Scramble for M

我有这个xml

<namedAnchor>
  {'Beacon': 'ORG', 'One': 'CARDINAL', 'Meadows': 'ORG', 'Congress': 'ORG', 'end of the month': 'DATE', 'second': 'ORDINAL', 'Tuesday': 'DATE', 'Wednesday': 'DATE', 'third': 'ORDINAL', 'New Yorker': 'NORP', 'Scramble for Medical Equipment Johnson City': 'ORG', 'US': 'GPE'}
</namedAnchor>

{'Beacon':'ORG','One':'CARDINAL','Meadows':'ORG','Congress':'ORG','end of the month':'DATE','second':'ORDINAL','DATE','DATE','third':'ORDINAL','NewYorker':'NORP','Scramble for Medical Equipment Johnson City':'ORG','US':'GPE'}
我需要打印在页面中以逗号分隔的所有键,我已经尝试过了

<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body>
<xsl:for-each select="tokenize(namedAnchor, ':')">
  <p><xsl:value-of select="." /></p>
</xsl:for-each>
</body>
</html>

我想要的是灯塔,一号,草地 有人能回答我的问题吗。>

这个
tokenize()
函数需要XSLT2.0
libxslt
是XSLT1.0处理器。但是,
libxslt
确实支持EXSLT
str:split()
扩展函数,因此您可以执行以下操作:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <output>
        <xsl:for-each select="str:split(normalize-space(namedAnchor), ', ')" >
            <item>
                <xsl:value-of select='translate(substring-before(., ":"), "{}&apos;", "")'/>
            </item>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

要获得:

<?xml version="1.0" encoding="UTF-8"?>
<output>
  <item>Beacon</item>
  <item>One</item>
  <item>Meadows</item>
  <item>Congress</item>
  <item>end of the month</item>
  <item>second</item>
  <item>Tuesday</item>
  <item>Wednesday</item>
  <item>third</item>
  <item>New Yorker</item>
  <item>Scramble for Medical Equipment Johnson City</item>
  <item>US</item>
</output>

信标
一个
草地
国会
月末
第二
星期二
星期三
第三
纽约人
争夺约翰逊市医疗设备
美国


请注意,这假设所有键都不包含模式
“,”
(从技术上讲,它们可以包含模式,因为它们被括在引号中)。要正确解析内容,您需要一个能够处理JSON的XSLT 3.0处理器。

请在所有XSLT问题中,始终说明您的处理器支持哪个版本的XSLT。很抱歉,这是我现在所说的
不是这样-看这里如何获得它:哦,那是
libxslt
@michael.hor257k,你能告诉我怎么看吗?因为JSON需要双引号来分隔字符串,并且显示的示例使用单引号,我认为XSLT 3及其JSON支持没有一种直接的方法。@MartinHonnen我相信这对我们来说是微不足道的在将结果馈送到
json-to-xml()
之前,请使用
translate()
。对于问题中的示例来说,这很简单,但如果任何值也包含一个引号,则很容易被打断。@MartinHonnen您能想到一种情况,即交换两个字符不起作用吗?假设内容只是Javascript对象文字,它可能是
{'foo':'这是“引用的”文本}
例如,但也
{foo:'这是“引用的”文本}
{foo]:'这是“引用的”文本}
和所有的变体,我认为很难使用translate将它们变形为
解析json
json到xml
接受的内容。