0
的結果,我想下面的XML轉換成JSON:XSLT:應用模板的另一個
<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)"}}
]}
您在輸入XML和XSLT中使用'& qout;'。那是故意的嗎?它應該是「&」還是「"」? – JLRishe 2013-03-11 14:07:17
是的,這很荒謬)但我沒有影響xml輸入 – 2013-03-11 15:04:02