2013-03-11 119 views
0

的結果,我想下面的XML轉換成JSON:XSLT:應用模板的另一個

<Table> 
    <Row> 
     <Column name="Column 1">outter quotation text and &amp;qout;inner quotation text&amp;qout;</Column> 
     <Column name="Column 2"> 

Some text with two 'new line' characters at the beginning (sometimes with &amp;qout;inner quotation text&amp;qout; also)</Column> 
    </Row> 
</Table> 

要有有效的JSON,我應該刪除所有「新行」字符(&#xA;)和替換&amp;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, '&#xA;'), '&#xA;')" /> 
     <xsl:if test="contains($pText, '&#xA;')"> 
      <xsl:text></xsl:text> 
      <xsl:call-template name="escapeNewLine"><xsl:with-param name="pText" select="substring-after($pText, '&#xA;')" /></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, '&amp;qout;'), '&amp;qout;')" /> 
      <xsl:if test="contains($pText, '&amp;qout;')"> 
       <xsl:text>\"</xsl:text> 
       <xsl:call-template name="escapeQuote"> 
        <xsl:with-param name="pText" select="substring-after($pText, '&amp;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 &amp;qout;inner quotation text&amp;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 &amp;qout;inner quotation text&amp;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)"}} 
    ]} 
+0

您在輸入XML和XSLT中使用'& qout;'。那是故意的嗎?它應該是「&」還是「"」? – JLRishe 2013-03-11 14:07:17

+0

是的,這很荒謬)但我沒有影響xml輸入 – 2013-03-11 15:04:02

回答

1

這就是你需要做什麼:

<xsl:call-template name="escapeQuote"> 
    <xsl:with-param name="pText"> 
    <xsl:call-template name="escapeNewLine" /> 
    </xsl:with-param> 
</xsl:call-template> 


也有可能在這裏有一些代碼重用,並將兩種換行符轉換爲\r\n,而不是將其刪除:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="text" /> 
    <xsl:template match="/"> 
    "data":[ 
    <xsl:apply-templates select="Table/Row" /> 
    ] 
    </xsl:template> 

    <xsl:template match="Row" name="RowData"> 
    { 
    <xsl:apply-templates select="Column" /> 
    } 
    </xsl:template> 

    <xsl:template match="Row[position() > 1]"> 
    <xsl:text>,</xsl:text> 
    <xsl:call-template name="RowData" /> 
    </xsl:template> 

    <xsl:template match="Column" name="ColumnData"> 
    <xsl:value-of select="concat('&quot;', @name, '&quot;:')" /> 

    <xsl:text>"</xsl:text> 
    <xsl:call-template name="escapeQuote"> 
     <xsl:with-param name="pText"> 
     <xsl:call-template name="escapeNewLine" /> 
     </xsl:with-param> 
    </xsl:call-template> 
    <xsl:text>"</xsl:text> 
    </xsl:template> 

    <xsl:template match="Column[position() > 1]"> 
    <xsl:text>,</xsl:text> 
    <xsl:call-template name="ColumnData" /> 
    </xsl:template> 

    <xsl:template name="escapeNewLine"> 
    <xsl:param name="pText" select="." /> 

    <xsl:call-template name="replace"> 
     <xsl:with-param name="pText"> 
     <xsl:call-template name="replace"> 
      <xsl:with-param name="pText" select="$pText" /> 
      <xsl:with-param name="from" select="'&#xA;'" /> 
      <xsl:with-param name="to" select="'\n'" /> 
     </xsl:call-template> 
     </xsl:with-param> 
     <xsl:with-param name="from" select="'&#xD;'" /> 
     <xsl:with-param name="to" select="'\r'" /> 
    </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="escapeQuote"> 
    <xsl:param name="pText" select="." /> 

    <xsl:call-template name="replace"> 
     <xsl:with-param name="pText" select="$pText" /> 
     <xsl:with-param name="from" select="'&amp;qout;'" /> 
     <xsl:with-param name="to" select="'\&quot;'" /> 
    </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="replace"> 
    <xsl:param name="pText" /> 
    <xsl:param name="from" /> 
    <xsl:param name="to" /> 

    <xsl:if test="string-length($pText) > 0"> 
     <xsl:value-of select="substring-before(concat($pText, $from), $from)" /> 
     <xsl:if test="contains($pText, $from)"> 
     <xsl:value-of select="$to" /> 
     <xsl:call-template name="replace"> 
      <xsl:with-param name="pText" select="substring-after($pText, $from)" /> 
      <xsl:with-param name="from" select="$from" /> 
      <xsl:with-param name="to" select="$to" /> 
     </xsl:call-template> 
     </xsl:if> 
    </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 
+0

這正是我需要的,謝謝 – 2013-03-11 15:19:54