我对这种XML标记有一个问题。
<Foo>value
</Foo>在应用XSLT之后,我得到了"foo":"value\n "
所以我使用了normalize-space(),它起了作用:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cbl="urn:chanel:names:specification:cbl:schema:xsd:CBLCore1.1.0"
xmlns="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="#all"
expand-text="yes"
>
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="//root">
<xsl:variable name="xml">
<map>
<string key="foo">{normalize-space(Foo)}</string>
<string key="bar">{normalize-space(Bar)}</string>
<map key="moo">
<map key="voo">
<string key="startDate">{normalize-space(Moo/Voo/StartDate)}</string>
<string key="endDate">{normalize-space(Moo/Voo/EndDate)}</string>
</map>
</map>
</map>
</xsl:variable>
<xsl:value-of select="xml-to-json($xml)"/>
</xsl:template>
</xsl:stylesheet>但是我不想为每个元素设置它,如何一次将它应用到所有文本节点?
我也试过normalize-space(xml-to-json($xml)),但效果不太好,这给了我"foo":"value\n "
发布于 2022-06-17 19:18:52
一种方法是建立
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="text()">
<xsl:value-of select="normalize-space()"/>
</xsl:template>然后你用
<xsl:template match="/">
<xsl:variable name="normalized-input">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="xml-to-json($normalized-input)"/>
</xsl:template>https://stackoverflow.com/questions/72663050
复制相似问题