【发布时间】:2016-10-28 21:17:20
【问题描述】:
我在处理一些 XSL 报告时遇到了问题。基本上,我将所有内容都输出到 CSV,这部分工作正常,但有时我需要将两个值放入同一个单元格但在不同的行中。
我尝试将每个字符串放在<p> 标签中,在两行之间放置一个<br />,在两个项目之间放置<xsl:text>&#10;</xsl:text> 和<xsl:text>&#13;</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="'"'" />
<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="'"'" />
<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中产生一个“回车”,你需要输出
<br/>。 -
@michael.hor257k 它们非常不同,是的,我不确定这一切是如何工作的,但如果我不使用 HTML 表格标签,所有东西都会被塞进 1 个单元格中。我已经尝试过
<br />,但这会将所有内容放入下面的新单元格中。 -
恐怕我不明白你的描述。如果您希望结果是在浏览器中显示的 HTML 页面,您应该使用 HTML 表格标签。如果你想要一个 .csv 文本文件,为什么不直接生成呢?似乎您的处理链中还有其他东西被您遗漏了。
-
@michael.hor257k 我们使用一种特定类型的软件来获取我的 XSL 和 XML 文件,然后创建输出。我可能不得不去和软件开发人员谈谈,看看我怎样才能得到正确的输出。感谢您的帮助。