XSLT + 替换双引号为转义序列

15
我有以下的xslt将数据转换成JQuery可接受的格式。但是,由于JSON不接受数据字符串中的双引号,我需要用转义序列\"替换它们。 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Rowsets>
    <Rowset>
        <Columns>
            <Column Description="Element_1" SQLDataType="12" />
            <Column Description="Element_2" SQLDataType="12" />
            <Column Description="Element_3" SQLDataType="93" />
        </Columns>
        <Row>
            <Element_1>test_data</Element_1>
            <Element_2>test_quo"te</Element_2>
            <Element_3>test_data</Element_3>
        </Row>
    </Rowset>
</Rowsets>

当前的 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java">
    <xsl:output method="text" media-type="text/csv" encoding="UTF-8"/>
    <xsl:param name="RowDelim">],</xsl:param>
    <xsl:param name="RowLast">]</xsl:param>
    <xsl:param name="RowStart">[</xsl:param>
    <xsl:param name="startBracket">{ </xsl:param>
    <xsl:param name="JQTableData">"aaData": [</xsl:param>
    <xsl:param name="JQTableEnd">] }</xsl:param>
    <xsl:param name="FieldDelim">,</xsl:param>
    <xsl:param name="StringDelim">"</xsl:param>
    <xsl:param name="DateFormat">yyyy-MM-dd HH:mm:ss</xsl:param>


    <xsl:template match="/">





        <xsl:for-each select="Rowsets">

            <xsl:for-each select="Rowset">
            <xsl:value-of select="$startBracket"/>
            <xsl:value-of select="$JQTableData"/>
                <xsl:variable name="CurrentColumns" select="Columns"/>
                <xsl:for-each select="Columns">
                    <xsl:for-each select="Column">

                        <xsl:if test="not(position() = last())">

                        </xsl:if>
                    </xsl:for-each>

                </xsl:for-each>
                <xsl:for-each select="Row">
                <xsl:value-of select="$RowStart"/>
                    <xsl:for-each select="*">
                        <xsl:variable name="ColName">
                            <xsl:value-of select="name(.)"/>
                        </xsl:variable>
                        <xsl:variable name="ColType">
                            <xsl:value-of select="$CurrentColumns/Column[@Name=$ColName]/@SQLDataType"/>
                        </xsl:variable>
                        <xsl:choose>
                            <xsl:when test="$ColType= '2' or $ColType= '3' or $ColType= '4' or $ColType= '5' or 

$ColType= '6' or $ColType= '7' or $ColType= '8' or $ColType= '-7'">
                                <xsl:value-of select="."/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="$StringDelim"/>
                                <xsl:choose>
                                    <xsl:when test="$ColType= '91' or $ColType= '92' or $ColType= '93'">
                                        <xsl:choose>
                                            <xsl:when test=". = 'TimeUnavailable'">
                                                <xsl:value-of select="."/>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                                                        </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:when>
                                    <xsl:otherwise>
                                    <xsl:choose>
                                        <xsl:when test=". = 'true'">
                                            <xsl:text>Y</xsl:text>
                                        </xsl:when>
                                        <xsl:when test=". = 'false'">
                                            <xsl:text>N</xsl:text>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:value-of select="."/>
                                        </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:otherwise>
                                </xsl:choose>
                                <xsl:value-of select="$StringDelim"/>
                            </xsl:otherwise>
                        </xsl:choose>
                        <xsl:if test="not(position() = last())">
                            <xsl:value-of select="$FieldDelim"/>
                        </xsl:if>
                    </xsl:for-each>
                            <xsl:if test="not(position() = last())">
                            <xsl:value-of select="$RowDelim"/>
                        </xsl:if>
                            <xsl:if test="position() = last()">
                            <xsl:value-of select="$RowLast"/>
                        </xsl:if>

                </xsl:for-each>
                <xsl:value-of select="$JQTableEnd"/>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>



</xsl:stylesheet>

当前输出:

{ "aaData": [["test_data","test_quo"te","test_data"]] }

预期输出:

{ "aaData": [["test_data","test_quo\"te","test_data"]] }

用户1130511:转义字符不是\(反斜杠)吗? - Dimitre Novatchev
用户1130511:提供的XSLT代码不规范--现在没有人能够使用它。请编辑问题并进行更正。 - Dimitre Novatchev
@DimitreNovatchev 哎呀,我复制了错误的版本。我已经更新了 XSLT,应该可以工作了。是的,应该是反斜杠。 - user1130511
用户1130511:我的回答对您有用吗? - Dimitre Novatchev
你好。 - Dimitre Novatchev
1个回答

28

将此模板添加到您的代码中

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

      <xsl:if test="string-length($pText) >0">
       <xsl:value-of select=
        "substring-before(concat($pText, '&quot;'), '&quot;')"/>

       <xsl:if test="contains($pText, '&quot;')">
        <xsl:text>\"</xsl:text>

        <xsl:call-template name="escapeQuote">
          <xsl:with-param name="pText" select=
          "substring-after($pText, '&quot;')"/>
        </xsl:call-template>
       </xsl:if>
      </xsl:if>
    </xsl:template>

然后更改:

<xsl:value-of select="."/>

目标:

<xsl:call-template name="escapeQuote"/>
您的完整转换现在变成了:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java">
        <xsl:output method="text" media-type="text/csv" encoding="UTF-8"/>
        <xsl:param name="RowDelim">],</xsl:param>
        <xsl:param name="RowLast">]</xsl:param>
        <xsl:param name="RowStart">[</xsl:param>
        <xsl:param name="startBracket">{ </xsl:param>
        <xsl:param name="JQTableData">"aaData": [</xsl:param>
        <xsl:param name="JQTableEnd">] }</xsl:param>
        <xsl:param name="FieldDelim">,</xsl:param>
        <xsl:param name="StringDelim">"</xsl:param>
        <xsl:param name="DateFormat">yyyy-MM-dd HH:mm:ss</xsl:param>


        <xsl:template match="/">

            <xsl:for-each select="Rowsets">

                <xsl:for-each select="Rowset">
                <xsl:value-of select="$startBracket"/>
                <xsl:value-of select="$JQTableData"/>
                    <xsl:variable name="CurrentColumns" select="Columns"/>
                    <xsl:for-each select="Columns">
                        <xsl:for-each select="Column">

                            <xsl:if test="not(position() = last())">

                            </xsl:if>
                        </xsl:for-each>

                    </xsl:for-each>
                    <xsl:for-each select="Row">
                    <xsl:value-of select="$RowStart"/>
                        <xsl:for-each select="*">
                            <xsl:variable name="ColName">
                                <xsl:value-of select="name(.)"/>
                            </xsl:variable>
                            <xsl:variable name="ColType">
                                <xsl:value-of select="$CurrentColumns/Column[@Name=$ColName]/@SQLDataType"/>
                            </xsl:variable>
                            <xsl:choose>
                                <xsl:when test="$ColType= '2' or $ColType= '3' or $ColType= '4' or $ColType= '5' or

    $ColType= '6' or $ColType= '7' or $ColType= '8' or $ColType= '-7'">
                                    <xsl:value-of select="."/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="$StringDelim"/>
                                    <xsl:choose>
                                        <xsl:when test="$ColType= '91' or $ColType= '92' or $ColType= '93'">
                                            <xsl:choose>
                                                <xsl:when test=". = 'TimeUnavailable'">
                                                    <xsl:value-of select="."/>
                                                </xsl:when>
                                                <xsl:otherwise>
                                                                                            </xsl:otherwise>
                                            </xsl:choose>
                                        </xsl:when>
                                        <xsl:otherwise>
                                        <xsl:choose>
                                            <xsl:when test=". = 'true'">
                                                <xsl:text>Y</xsl:text>
                                            </xsl:when>
                                            <xsl:when test=". = 'false'">
                                                <xsl:text>N</xsl:text>
                                            </xsl:when>
                                            <xsl:otherwise>
                                              <xsl:call-template name="escapeQuote"/>
                                                <!-- <xsl:value-of select="."/> -->
                                            </xsl:otherwise>
                                            </xsl:choose>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                    <xsl:value-of select="$StringDelim"/>
                                </xsl:otherwise>
                            </xsl:choose>
                            <xsl:if test="not(position() = last())">
                                <xsl:value-of select="$FieldDelim"/>
                            </xsl:if>
                        </xsl:for-each>
                                <xsl:if test="not(position() = last())">
                                <xsl:value-of select="$RowDelim"/>
                            </xsl:if>
                                <xsl:if test="position() = last()">
                                <xsl:value-of select="$RowLast"/>
                            </xsl:if>

                    </xsl:for-each>
                    <xsl:value-of select="$JQTableEnd"/>
                </xsl:for-each>
            </xsl:for-each>
        </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, '&quot;'), '&quot;')"/>

           <xsl:if test="contains($pText, '&quot;')">
            <xsl:text>\"</xsl:text>

            <xsl:call-template name="escapeQuote">
              <xsl:with-param name="pText" select=
              "substring-after($pText, '&quot;')"/>
            </xsl:call-template>
           </xsl:if>
          </xsl:if>
        </xsl:template>

</xsl:stylesheet>

当应用此转换于提供的 XML 文档时:

<Rowsets>
    <Rowset>
        <Columns>
            <Column Description="Element_1" SQLDataType="12" />
            <Column Description="Element_2" SQLDataType="12" />
            <Column Description="Element_3" SQLDataType="93" />
        </Columns>
        <Row>
            <Element_1>test_data</Element_1>
            <Element_2>test_quo"te</Element_2>
            <Element_3>test_data</Element_3>
        </Row>
    </Rowset>
</Rowsets>

所期望的、正确的结果已经产生:

{ "aaData": [["test_data","test_quo\"te","test_data"]] }

抱歉,我对XSLT相对较新。是否有可能通过添加某些属性自动将此模板应用于“body/row”的所有值? - James Cazzetta
1
@JamesCazzetta,需要调用命名模板。我强烈建议您参加XSLT培训课程,例如:http://www.pluralsight.com/courses/xslt-foundations-part1 - Dimitre Novatchev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接