我想将以下 XML 转换为 JSON:
<Table>
<Row>
<Column name="Column 1">outter quotation text and &qout;inner quotation text&qout;</Column>
<Column name="Column 2">
Some text with two 'new line' characters at the beginning (sometimes with &qout;inner quotation text&qout; also)</Column>
</Row>
</Table>
要获得有效的 json,我应该删除所有“换行符”字符 ( 

) 并替换&qout;
为\"
.
看起来这段代码有助于删除“新行”:
<xsl:template name="escapeNewLine">
<xsl:param name="pText" select="." />
<xsl:if test="string-length($pText) > 0">
<xsl:value-of select="substring-before(concat($pText, '
'), '
')" />
<xsl:if test="contains($pText, '
')">
<xsl:text></xsl:text>
<xsl:call-template name="escapeNewLine"><xsl:with-param name="pText" select="substring-after($pText, '
')" /></xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
和类似于替换引号的东西:
<xsl:template name="escapeQuote">
<xsl:param name="pText" select="." />
<xsl:if test="string-length($pText) > 0">
<xsl:value-of select="substring-before(concat($pText, '&qout;'), '&qout;')" />
<xsl:if test="contains($pText, '&qout;')">
<xsl:text>\"</xsl:text>
<xsl:call-template name="escapeQuote">
<xsl:with-param name="pText" select="substring-after($pText, '&qout;')" />
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
我的样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" />
<xsl:template match="/">
"data":[
<xsl:for-each select="Table/Row">
<xsl:if test="position() > 1">,</xsl:if>
{
<xsl:for-each select="Column">
<xsl:if test="position() > 1">,</xsl:if>
"<xsl:value-of select="@name" />":
"
<xsl:call-template name="escapeQuote" />
<xsl:call-template name="escapeNewLine" />
<!-- <xsl:value-of select="." /> -->
"
</xsl:for-each>
}
</xsl:for-each>]
</xsl:template>
</xsl:stylesheet>
我的问题是如何将两个模板应用到同一个节点?所以而不是这个:
{"data":[
{"Column 1":"outter quotation text and \"inner quotation text\" outter quotation text and &qout;inner quotation text&qout;"},
{"Column 2":"Some text with two 'new line' characters at the beginning (maybe with \"inner quotation text\" also)
Some text with two 'new line' characters at the beginning (maybe with &qout;inner quotation text&qout; also)"}}
]}
我可以得到这个结果:
{"data":
[{"Column 1":"outter quotation text and \"inner quotation text\""},
{"Column 2":"Some text with two 'new line' characters at the beginning (maybe with \"inner quotation text\" also)"}}
]}