Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

xml - Stuck on escape character usage

In the haste and frustration of the day, I didn't obviously post enough relevant info, so let me rephrase the question. Thanks for your patience.

I have some XML that is returning the following tags:

etc...
<tr>
            <td class="Label Center">22</td>
            <td Indentation="4" class="Label Stub Left">Listings</td>
<tr>
   <td class="Label Center">23</td>
   <td Indentation="6" class="Label Stub Left">Notes&lt;sup&gt;1&lt;/sup&gt;</td>
</tr>
etc...

I process them the following way, everything is fine, except for one thing:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:param name="TABLENUM"/>

<xsl:template match="/">
<xsl:for-each select="/Response/MakeStory/div/div[@class='Table'][$TABLENUM]">
    <h3 style="margin:2px; padding:0;"><xsl:value-of select="div [@class='TableTitle']"/></h3>
    <h4 style="margin:2px; padding:0;"><em><xsl:value-of select="div[@class='TableSubTitle']"/></em></h4>
    <xsl:for-each select="div[@class='HtmlTable']/table">
        <table cellpadding="3" cellspacing="0">
                          <xsl:copy-of select="thead"/>
                            <xsl:apply-templates select="tbody"/>
        </table>
    </xsl:for-each>
</xsl:for-each>
</xsl:template>


<xsl:template match="tbody">
  <xsl:copy>
    <xsl:apply-templates select="tr"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="tr">
    <xsl:copy>
      <xsl:apply-templates select="td"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="td[contains(@class, 'Left')]">
  <td align="left" style="padding-left: {@Indentation * 3}px;">
        <xsl:copy-of select="node()"/>            
  </td>
</xsl:template>

<xsl:template match="td[contains(@class, 'Center')]">
  <td align="center">
    <xsl:copy-of select="node()" />
  </td>
</xsl:template>

The problem I have which is with the example above, is when the items are outputted, the escape tags are taken as literals and write out Notes<sup>1</sup> literally, instead of superscripting the number like this Notes1.

I've tried numerous ways including, xsl text escape characters set to no wrapped around the node, trying to apply a template instead of using the copy-of select node option with a choose condition looking for the escape character, but I am unfortunately not having any luck.

What would be the best approach to handle this. I am sure this is really simple, but my limited knowledge of XSLT isn't really getting it done. Any information would be appreciated.

Oh I "think" I have XSLT 2, but I am not sure.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To minimize the example to the problem, let's consider the following XML source:

<tr>
   <td>Notes&lt;sup&gt;1&lt;/sup&gt;</td>
</tr>

The following stylesheet:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="tr">
    <tr>
        <xsl:apply-templates select="td"/>
    </tr>
</xsl:template>

<xsl:template match="td">
    <td>
        <xsl:value-of select="." disable-output-escaping="yes"/>
    </td>
</xsl:template>

</xsl:stylesheet>

will return:

<?xml version="1.0" encoding="UTF-8"?>
<tr>
  <td>Notes<sup>1</sup></td>
</tr>

which in HTML will be rendered as:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...