Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XLST在转换为html后创建一个空空间_Html_Xml_Xslt - Fatal编程技术网

XLST在转换为html后创建一个空空间

XLST在转换为html后创建一个空空间,html,xml,xslt,Html,Xml,Xslt,我不明白 我的xml输入: <?xml version="1.0" encoding="UTF-8"?> <results> <error file="mixed.cpp" line="11" id="unreadVariable" severity="style" msg="Variable 'wert' is assigned a value that is never used."/> <error

我不明白

我的xml输入:

    <?xml version="1.0" encoding="UTF-8"?>
    <results>
    <error file="mixed.cpp" line="11" id="unreadVariable" severity="style"          msg="Variable 'wert' is assigned a value that is never used."/>
    <error file="mixed.cpp" line="13" id="unassignedVariable"         severity="style" msg="Variable 'b' is not assigned a value."/>
    <error file="mixed.cpp" line="11" id="arrayIndexOutOfBounds" severity="error" msg="Array 'wert[2]' accessed at index 3, which is out of bounds."/>
    <error file="mixed.cpp" line="15" id="uninitvar" severity="error" msg="Uninitialized variable: b"/>
    <error file="mixed.cpp" line="5" id="unusedFunction" severity="style" msg="The function 'func' is never used."/>
    <error file="*" line="0" id="unmatchedSuppression" severity="style" msg="Unmatched suppression: missingIncludeSystem"/>
    </results>

使用此xsl文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:template match="error">
    <tr>
      <td><xsl:value-of select="@file"/></td>
      <td><xsl:value-of select="@line"/></td>
      <td><xsl:value-of select="@test"/></td>
      <td><xsl:value-of select="@severity"/></td>
      <td><xsl:value-of select="@msg"/></td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

但我得到的第一行是空的:

empty line
<tr><td>mixed.cpp</td><td>11</td><td/><td>style</td><td>Variable 'wert' is assigned a value that is never used.</td></tr>
空行
mixed.CPP11STYLE变量“wert”被分配了一个从未使用过的值。

空行从何而来?

默认模板用于不匹配
错误的模板,默认模板仅输出文本。由于您有空白文本节点,并且您没有匹配
结果
,因此
结果
(以及
错误
前后)中的空白将成为输出的一部分

有多种方法可以解决这个问题。一种典型的方法是编写一个低优先级的模板来匹配您不想匹配的文本。也就是说,如果您添加以下内容,您的空白将消失:

<xsl:template match="text()" />
第三种方法是添加一个空格剥离声明,但如果实际样式表更大并且依赖于其他地方的空格,则这可能会影响输入XML。这只会去除
结果
元素上的空白:

<xsl:strip-space elements="results"/>

所有这三个解决方案都有效,这取决于您的整个项目,哪一个最合适

请记住,在XSLT1.0和XSLT2.0中,不匹配的节点将由默认模板(不可见)匹配,并只输出该节点的文本值。在XSLT 3.0中,您可以更好地控制此过程:

<!-- XSLT 3.0 only -->
<xsl:mode on-no-match="shallow-skip" />


在xsl:output之前添加
。试试看。你是最棒的;)感谢您的结构,在每个匹配的
错误
之间还应该有一个尾随空行和一个空行。删除空白后,
将不再从新行开始,除非在
xsl:output
上添加
indent=“yes”
。另外,如果目标是html,请使用
method=“html”
。它将处理(非)自动关闭元素,否则浏览器将阻塞这些元素。也就是说,

。还请记住,如果有多个
错误
,当前样式表将不会生成格式良好的XML输出,因为它将输出多个
tr
元素,而不包含根级别的元素。像
这样的附加模板可以解决此问题(通过创建根
tbody
)和原始空行(通过不向空白文本节点应用任何模板)。
<!-- XSLT 3.0 only -->
<xsl:mode on-no-match="shallow-skip" />