Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
Xml 如何在xslt中不为具有属性的元素输出空白_Xml_Xslt_Whitespace_Xslt 1.0 - Fatal编程技术网

Xml 如何在xslt中不为具有属性的元素输出空白

Xml 如何在xslt中不为具有属性的元素输出空白,xml,xslt,whitespace,xslt-1.0,Xml,Xslt,Whitespace,Xslt 1.0,我有一个xslt文档,我想输出一个带有一些属性的锚(a)标记,这些属性的值取决于其他内容。。因此,我在xsl:attribute标记下面使用了choose/if(反之亦然)。因此,我的代码如下所示: <a href="/somepage.html"> <xsl:if test="current_page='this_page'"> <xsl:attribute name='class'>active</xsl:attribute> &

我有一个xslt文档,我想输出一个带有一些属性的锚(a)标记,这些属性的值取决于其他内容。。因此,我在xsl:attribute标记下面使用了choose/if(反之亦然)。因此,我的代码如下所示:

<a href="/somepage.html">
  <xsl:if test="current_page='this_page'">
   <xsl:attribute name='class'>active</xsl:attribute>
  </xsl:if>
My Page
</a>

然而,问题是,在输出html中,所有的换行符/空格都在那里,这使得我的链接在它的左边有一个额外的空格(并且它加了下划线,使它很明显)。 因此,显而易见的解决方案是这样做:

<a href="/somepage.html"><xsl:if test="current_page='this_page'"><xsl:attribute name='class'>active</xsl:attribute></xsl:if>My Page</a>

为了摆脱空间。上面的代码没什么大不了的,但我的实际页面有更多的逻辑,这让它非常难看。我唯一能想到的另一件事就是把逻辑放在链接生成之外,但是我会重复更多的事情,并且必须创建更多的变量。这是合理的,但仍不完全理想。
这只是我想做这件事的一个例子,它发生了好几次,所以我只是想知道是否有其他方法可以解决这个问题。

也许您可以在XSLT文档的开头使用它:

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

更新,此功能:

<a href="/somepage.html">
  <xsl:if test="current_page='this_page'">
   <xsl:attribute name='class'>active</xsl:attribute>
  </xsl:if>
  <xsl:text>My Page</xsl:text>
</a>

样式表顶部的一个简单的
xsl:strip空间是否足够

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html" indent="yes"/>

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

  ...

</xsl:stylesheet>

...

这将从输入中的元素中去除空白。。在本例中,我实际上是在生成一个标记,我只想在innerhtmli中不留空格,在
xsl:stylesheet
之后添加
xsl:strip space
,但是
错误加载样式表:解析XSLT样式表失败。