【问题标题】:Carriage return in CSV cell using XSL使用 XSL 在 CSV 单元格中回车
【发布时间】:2016-10-28 21:17:20
【问题描述】:

我在处理一些 XSL 报告时遇到了问题。基本上,我将所有内容都输出到 CSV,这部分工作正常,但有时我需要将两个值放入同一个单元格但在不同的行中。

我尝试将每个字符串放在<p> 标签中,在两行之间放置一个<br />,在两个项目之间放置<xsl:text>
</xsl:text><xsl:text>
</xsl:text>,但是它们最终总是在不同的单元格中。

下面是 XSL 中用于将两个或多个项目放在一个单元格中的部分,每个项目之间有回车符。

<xsl:template name="getRoutingRules">
 <xsl:param name="itemID" />
 <xsl:for-each select="/aka-Architect/Schema/Relationships/OwnerTerm[@ID=$itemID]">
  <xsl:if test="@Entity='Subject'">
    <xsl:for-each select="./Routing_Rules/*">
      <xsl:if test="@Entity='Routing Rules'">
          <xsl:call-template name="replace-string">
            <xsl:with-param name="text" select="@Name"/>
            <xsl:with-param name="replace" select="'&quot;'" />
            <xsl:with-param name="with" select="''" />
          </xsl:call-template>
        <br />
        <xsl:text>
      </xsl:text>
      </xsl:if>

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

<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
  <xsl:when test="contains($text,$replace)">
    <xsl:value-of select="substring-before($text,$replace)"/>
    <xsl:value-of select="$with"/>
    <xsl:call-template name="replace-string">
      <xsl:with-param name="text"
          select="substring-after($text,$replace)"/>
      <xsl:with-param name="replace" select="$replace"/>
      <xsl:with-param name="with" select="$with"/>
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="$text"/>
  </xsl:otherwise>
</xsl:choose>

下面是完整的 XSL

<?xml version="1.0"?>
<!--CSV-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="http://www.stylusstudio.com/xquery">
<xsl:template match="/aka-Architect">
<html>
  <head/>
  <body>

    <xsl:apply-templates select="Schema/Hierarchy/TopTerm"/>
    <!--xsl:apply-templates/-->
  </body>
</html>
</xsl:template>

<xsl:template name="GetTopTerm" match="Schema/Hierarchy/TopTerm">
<table>
  <tr>
    <td>
      <xsl:text>BCS Path</xsl:text>
    </td>
    <td>
      <xsl:text>Descriptor</xsl:text>
    </td>
    <td>
      <xsl:text>Descriptor Type</xsl:text>
    </td>
    <td>
      <xsl:text>Description</xsl:text>
    </td>
    <td>
      <xsl:text>Routing Rules</xsl:text>
    </td>
  </tr>
  <!--<xsl:call-template name="GetFunctions" />
  <xsl:call-template name="GetActivities" />-->
  <xsl:call-template name="GetSubjects" />
</table>
</xsl:template>



<!---Get Function -->
<xsl:template name="GetFunctions">
<xsl:for-each select="/aka-Architect/Entities/Function/Items">
  <xsl:for-each select="./*">
    <tr>
      <td>
        <xsl:value-of select=".//ixPath" />
      </td>
      <td>
        <xsl:value-of select=".//Name"/>
      </td>
      <td>
        <xsl:value-of select=".//BCSLevel"/>
      </td>
      <td>
        <xsl:value-of select=".//Description"/>
      </td>
    </tr>
  </xsl:for-each>
</xsl:for-each>
</xsl:template>


<!---Get Activity -->
<xsl:template name="GetActivities">
<xsl:for-each select="/aka-Architect/Entities/Activity/Items">
  <xsl:for-each select="./*">
    <tr>
      <td>
        <xsl:value-of select=".//ixPath" />
      </td>
      <td>
        <xsl:value-of select=".//Name"/>
      </td>
      <td>
        <xsl:value-of select=".//BCSLevel"/>
      </td>
      <td>
        <xsl:value-of select=".//Description"/>
      </td>
    </tr>
  </xsl:for-each>
</xsl:for-each>
</xsl:template>

<!---Get Subject -->
<xsl:template name="GetSubjects">
<xsl:for-each select="/aka-Architect/Entities/Subject/Items">
  <xsl:for-each select="./*">
    <tr>
      <td>
        <xsl:value-of select=".//ixPath" />
      </td>
      <td>
        <xsl:value-of select=".//Name"/>
      </td>
      <td>
        <xsl:value-of select=".//BCSLevel"/>
      </td>
      <td>
        <xsl:value-of select=".//Description"/>
      </td>
      <td>
        <xsl:call-template name="getRoutingRules">
          <xsl:with-param name="itemID" select='@ID' />
        </xsl:call-template>
      </td>
    </tr>
  </xsl:for-each>
</xsl:for-each>
</xsl:template>



<!-- FIND ROUTING RULES -->
<xsl:template name="getRoutingRules">
<xsl:param name="itemID" />
<xsl:for-each select="/aka-Architect/Schema/Relationships/OwnerTerm[@ID=$itemID]">
  <xsl:if test="@Entity='Subject'">
    <xsl:for-each select="./Routing_Rules/*">
      <xsl:if test="@Entity='Routing Rules'">
        <p>
          <xsl:call-template name="replace-string">
            <xsl:with-param name="text" select="@Name"/>
            <xsl:with-param name="replace" select="'&quot;'" />
            <xsl:with-param name="with" select="''" />
          </xsl:call-template>
        </p>
        <xsl:text>
      </xsl:text>
      </xsl:if>

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


<!-- replace " with ' -->
<xsl:template name="replace-string">
 <xsl:param name="text"/>
 <xsl:param name="replace"/>
 <xsl:param name="with"/>
 <xsl:choose>
  <xsl:when test="contains($text,$replace)">
    <xsl:value-of select="substring-before($text,$replace)"/>
    <xsl:value-of select="$with"/>
    <xsl:call-template name="replace-string">
      <xsl:with-param name="text"
          select="substring-after($text,$replace)"/>
      <xsl:with-param name="replace" select="$replace"/>
      <xsl:with-param name="with" select="$with"/>
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="$text"/>
  </xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<!-- (c) 2016. CorpMem Business Solutions. All rights reserved.-->

感谢任何和所有的帮助。

干杯, 约翰

编辑:应要求添加了完整的 XSL。

【问题讨论】:

  • 您的问题是关于 CSV - 但您的代码会生成一个 HTML 表格。这是两个非常不同的东西。如果你想在HTML中产生一个“回车”,你需要输出&lt;br/&gt;
  • @michael.hor257k 它们非常不同,是的,我不确定这一切是如何工作的,但如果我不使用 HTML 表格标签,所有东西都会被塞进 1 个单元格中。我已经尝试过&lt;br /&gt;,但这会将所有内容放入下面的新单元格中。
  • 恐怕我不明白你的描述。如果您希望结果是在浏览器中显示的 HTML 页面,您应该使用 HTML 表格标签。如果你想要一个 .csv 文本文件,为什么不直接生成呢?似乎您的处理链中还有其他东西被您遗漏了。
  • @michael.hor257k 我们使用一种特定类型的软件来获取我的 XSL 和 XML 文件,然后创建输出。我可能不得不去和软件开发人员谈谈,看看我怎样才能得到正确的输出。感谢您的帮助。

标签: xml csv xslt


【解决方案1】:

来自specification

  1. 包含换行符 (CRLF)、双引号和逗号的字段 应该用双引号括起来。例如:

    "aaa","b CRLF

   bb","ccc" CRLF
   zzz,yyy,xxx

所以你只需要确保你的单元格用引号分隔,你可以在其中放置换行符。

【讨论】:

  • 如果我添加引号,它们只会打印在屏幕上。我能够将数据放入 CSV 中的单元格的唯一方法是在我的 XSL 中使用 HTML 表格元素。
  • 他们这样做是因为 CSV 库的工作是在创建 CSV 时添加它们。你应该添加你的代码。
  • 添加了 XSL。希望它有所帮助。当我添加引号时,它会显示在屏幕上。
猜你喜欢
  • 2011-04-30
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 2014-10-21
  • 1970-01-01
  • 2022-12-05
相关资源
最近更新 更多