【问题标题】:How to generate a comma in XSL using XML file如何使用 XML 文件在 XSL 中生成逗号
【发布时间】:2020-12-27 11:51:43
【问题描述】:

我有以下 XML 文件:

<table>
<tr> 
    <th>Month</th> 
    <th>March, 2020</th>
    <th>XAU Position</th> 
    <th></th> 
    <th>USD Position</th> 
    <th></th> 
</tr> 
<tr>
    <th2>Trade Date</th2> 
    <th2>LBMA AM FIXING</th2> 
    <th2>Positive</th2> 
    <th2>Negative</th2> 
    <th2>Positive</th2> 
    <th2>Negative</th2> 
</tr>
<tr> 
    <td align="center"> Ch_H907</td> 
    <td align="center"> 907</td> 
    <td align="center"> DXM09902</td> 
    <td align="center"> Shipped</td> 
    <td align="center"> USPS</td> 
    <td align="center"> </td> 
</tr> 

还有如下style.xsl文件:

<?xml version="1.0"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:fo="http://www.w3.org/1999/XSL/Format" >
 <xsl:output method="text" omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="/">
  <html>
  <body>
  <table border="1">
  <tr>
  <xsl:for-each select="//tr">
    <xsl:for-each select="th">
        <xsl:if test="position()> 1">,</xsl:if>
        <xsl:value-of select="."/>
    </xsl:for-each> 
    </xsl:for-each>
  </tr>
 <xsl:text>&#xA;</xsl:text>
 <tr>
   <xsl:for-each select="//tr">
    <xsl:for-each select="th2">
        <xsl:if test="position() > 1">,</xsl:if>
        <xsl:value-of disable-output-escaping="yes" select="."/>
    </xsl:for-each> 
    </xsl:for-each>
  </tr>
   <tr>
   <xsl:for-each select="//tr">
    <xsl:for-each select="td">
        <xsl:if test="position() > 1">,</xsl:if>
        <xsl:value-of select="."/>
       </xsl:for-each>
     <xsl:text>&#xA;</xsl:text>
     </xsl:for-each>
    </tr>
  </table>
  </body>
 </html>
</xsl:template>
  </xsl:stylesheet>

我的问题是:我想用逗号取第二个:2020 年 3 月,并按原样显示逗号,我的问题是,当我从style.xsl 文件呈现逗号并将值拆分为 Excel 工作表中的两个单独的列。如何按原样呈现逗号?

【问题讨论】:

  • 如果您正在生成 CSV,并且您的输出方法是 text,那么您需要所有 HML 标记做什么?

标签: java excel xml xslt


【解决方案1】:

要转义逗号,整个短语需要用双引号括起来。 "&lt;xsl:value-of select="."/&gt;"

例如:

<?xml version="1.0"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:fo="http://www.w3.org/1999/XSL/Format" >
 <xsl:output method="text" omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="/">
  <html>
  <body>
  <table border="1">
  <tr>
  <xsl:for-each select="//tr">
    <xsl:for-each select="th">
        <xsl:if test="position()> 1">,</xsl:if>
        "<xsl:value-of select="."/>"
    </xsl:for-each> 
    </xsl:for-each>
  </tr>
 <xsl:text>&#xA;</xsl:text>
 <tr>
   <xsl:for-each select="//tr">
    <xsl:for-each select="th2">
        <xsl:if test="position() > 1">,</xsl:if>
        "<xsl:value-of disable-output-escaping="yes" select="."/>"
    </xsl:for-each> 
    </xsl:for-each>
  </tr>
   <tr>
   <xsl:for-each select="//tr">
    <xsl:for-each select="td">
        <xsl:if test="position() > 1">,</xsl:if>
        "<xsl:value-of select="."/>"
       </xsl:for-each>
     <xsl:text>&#xA;</xsl:text>
     </xsl:for-each>
    </tr>
  </table>
  </body>
 </html>
</xsl:template>
  </xsl:stylesheet>

【讨论】:

  • 如果您正在生成 CSV,并且您的输出方法是 text,那么您需要所有 HML 标记做什么?
猜你喜欢
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多