使用 XSL 样式表打开 XML 派生数据时创建的不需要的行

Unwanted Rows Created When Opening XML Derived Data Using XSL Stylesheet

提问人:SpaceStrip 提问时间:11/2/2023 最后编辑:TylerHSpaceStrip 更新时间:11/3/2023 访问量:51

问:

我们使用 XSL 样式表从 RestAPI 命令中获得数据,该样式表以格式良好的方式填充浏览器。在浏览器情况下,“描述”字段/列占用一 (1) 个块/单元格,无论存在多少行分隔符、段落元素或列表。

当将浏览器 XML 导出到具有旧 XLS 扩展名的文件并提供相同的 XSL 样式表(为简单起见,在同一文件夹中)时,创建的 Excel 工作表(带表格)的行为与本地样式表的预期相同,但只要存在换行符、段落元素或列表,就会在“说明”列中生成额外的行(为简单起见,未提及其他可能性)。有趣的是,表中存在额外行的所有其他单元格都会自动合并。此外,任何具有多行数据的列都可能发生这种情况。

在创建一个 Excel 表格时,我们可以对所有数据(带有关联的“描述”)进行排序,我们现在遇到了一个不可能的问题,因为只有描述的第一个块(在中断元素之前)与排序的行一起携带,而额外行中的其余数据则被孤立为不可排序。

显然,我们正在寻找一种解决方案,在Excel中打开时,所有多行“描述”数据都保留在单个单元格中。我提供了 Excel 97“Multiline_Excel_Issue.xls”XML 数据和“TableXMLstylesheet.xsl”样式表的完整代码,它们需要位于同一文件夹中。打开 XLS 文件时,它会要求提供样式表并抱怨一些事情,并在网格线关闭的情况下以保护模式打开。这可以在信任设置中克服。

我怀疑对行<xsl:value-of disable-output-escaping=“yes” select=“进行了一些修改。XSL 样式表末尾的 /> 将是将所有多行“描述”数据组合在一起的关键,但欢迎所有想法。(顺便说一句,每个人都应该了解,如果多行文本用引号括起来并粘贴到 Excel 单元格中,它的行为和停留在单个单元格中)。

这是 Excel 工作表的样子,其中生成了额外的不需要的行。

Current unwanted extra rows

如果样式表是固定的(没有生成额外的不需要的行),这就是 Excel 工作表的所需输出的样子。

Desired Excel Output

对于 Excel 97 文件的 RestAPI XML 数据,请复制下面的块并另存为“Multiline_Excel_Issue.xls”

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="TableXMLstylesheet.xsl"?>
<Assets>
  <Asset id="Single:01">
    <Attribute name="Description">This is a statement with a single line "as in single" line.&amp;nbsp; At this time I have no plans to change this line.&lt;br&gt;</Attribute>
    <Attribute name="Name">Single line has no issues</Attribute>
  </Asset>
  <Asset id="Multi:01">
    <Attribute name="Description">This description has multiple lines separated by these BR line break objects. I want all the rows generated in Excel to go away/gone never to be seen. This does not show up in the browser. How can we fix the xsl stylesheet??.&lt;br&gt;&lt;br&gt;Thank you for helping me understand the issue.&amp;nbsp; It will be a great accomplishement to fix this problem.&lt;br&gt;</Attribute>
    <Attribute name="Name">Multiline HTML with line break elements</Attribute>
  </Asset>
  <Asset id="Empty:01">
    <Attribute name="Description" />
    <Attribute name="Name">This has no lines in Description</Attribute>
  </Asset>
  <Asset id="Multi:02">
    <Attribute name="Description">&lt;p&gt;See https://someuselesswebsitedonotbotherclicking.com/dummy/index.php?title=Title_one/Status_one#Making_it_Work&lt;/p&gt;
      &lt;p&gt;&amp;nbsp;&lt;/p&gt;
      &lt;p&gt;It would be great if this multiline example would show up in a single cell in Excel. Need to determine the cause/issue.&lt;/p&gt;</Attribute>
    <Attribute name="Name">Multiline HTML with paragraph elements and URLs</Attribute>
  </Asset>
  <Asset id="Multi:03">
    <Attribute name="Description">&lt;p&gt;This description has some multiline auto-generated numbering:&lt;/p&gt;
      &lt;ol&gt;
      &lt;li&gt;This is the first line of the auto-numbered section/region.&lt;/li&gt;
      &lt;li&gt;And this is the second&lt;/li&gt;
      &lt;li&gt;And so on and so on and so on (will it stop?).&lt;/li&gt;
      &lt;/ol&gt;</Attribute>
    <Attribute name="Name">Multiline HTML with auto line numbering elements</Attribute>
  </Asset>
</Assets>

对于 XSL 样式表,复制下面的块并另存为“TableXMLstylesheet.xsl”

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <html>
            <head>
                <title>Multiline Excel Issue</title>
                <style type="text/css">
                    .columnheader {
                                background: rgba(200, 200, 200, .8);
                                border-style: solid;
                                border-color: blue;
                            }
                    .column {
                                background: rgba(230, 230, 230, 0);
                                vertical-align: top;
                                border-style: solid;
                                border-color: red;
                            }
                </style>
            </head>
            <body>
                <xsl:apply-templates select="Assets" />
                <table>
                    <thead>
                        <tr>
                            <td class="columnheader">AssetID</td>
                            <xsl:for-each select="Assets/Asset[1]/child::*">
                                <td class="columnheader"><xsl:value-of select="@name"/></td>
                            </xsl:for-each>
                        </tr>
                    </thead>
                    <tbody>
                        <xsl:apply-templates select="Assets/Asset" mode="Row" />
                    </tbody>
                </table>
            </body>
        </html>     
    </xsl:template>
    <xsl:template match="Assets" />
    <xsl:template match="Asset" mode="Row">
        <tr>
            <td class="column">
                <a><xsl:value-of select="@id"/></a>
            </td>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>
    <xsl:template match="Attribute">
        <td class="column">
            <xsl:value-of disable-output-escaping="yes" select="."/>
        </td>
    </xsl:template> 
</xsl:stylesheet>

将所需结果 XLS 文件保存为 HTML 时生成的 HTML 片段。这是针对单元格 B3:

<td class="xl68" width="256" style="border-top:none;border-left:none;width:192pt">This
  description has multiple lines separated by these BR line break objects. I
  want all the rows generated in Excel to go away/gone never to be seen. This
  does not show up in the browser. How can we fix the xsl stylesheet??.<br>
    <br>
    Thank you for helping me understand the issue.&nbsp; It will be a great
  accomplishement to fix this problem.</td>

Excel XML XSLT

评论

0赞 michael.hor257k 11/2/2023
不确定我是否理解你的问题是关于什么的。这仅仅是删除源代码的(转义的)HTML 中包含的元素吗?这应该不会太难(尽管如果可以使用 XSLT 3.0 处理器会容易得多)。<br>
0赞 SpaceStrip 11/2/2023
@michael.hor257k 感谢您查看该帖子。我们不想操纵或删除任何东西。想象一下,数以百万计的资产具有数百万个孤立行(而不仅仅是这 5 个资产示例)。我们需要修复样式表,以便描述数据保留在每个资产行的一个单元格中。
0赞 michael.hor257k 11/2/2023
根据定义,XSL 转换操作 XML 输入数据。我的问题是样式表需要如何操作数据,以便转换结果满足您的要求。如果您不知道这一点,那么您的问题与 XSLT 无关(至少现在还不是)。如果您这样做,请编辑您的问题,并在转换结束时添加您想要获得的确切结果(作为 HTML 代码)。
0赞 SpaceStrip 11/2/2023
@michael.hor257k 我是新手,正在尝试添加一张我们希望输出外观的图片。所有 AssetID、Description 和 Name 各占一行。我希望一张照片足以传达我们的愿望。我将努力附上图片。
0赞 michael.hor257k 11/2/2023
不,图片对此毫无用处。找出 HTML 代码需要是什么,以便 Excel 中的结果是您需要的。

答: 暂无答案