需要删除不需要的空间内容元素

时间:2017-02-28 11:52:26

标签: xml xslt xslt-2.0

我在元素中有空格,因为那个空格,我得到验证错误。

我的输入xml:

<div><a href="https://tneb.com"><img src="https://tnee.com"/></a>
<div> </div>
</div>

我使用的XSL:

<xsl:template match="div">
<fig>
<xsl:apply-templates/>    
</fig>
</xsl:template>

<xsl:template match="a">
<xsl:element name="xref">
<xsl:if test="@href">
<xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
</xsl:if>
</xsl:element>
</xsl:template>

<xsl:template match="img">
<xsl:element name="image">
<xsl:if test="@src">
<xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
</xsl:if>
</xsl:element>
</xsl:template>

输出我得到:

<fig>
<xref href="https://tneb.com">
<image href="https://tnee.com"/>
</xref> </fig>

但我需要预期的输出如下:

<fig>
<xref href="https://tneb.com">
<image href="https://tnee.com"/>
</xref></fig>

由于这是不必要的,我在标签之间有空间。所以我得到验证错误。请提供给我的编码。提前致谢

1 个答案:

答案 0 :(得分:0)

让我们从XSLT的修正开始:你写道:

let max: number = numbers[0];

应该改为:

<xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>

现在回到你的问题:把

<xsl:attribute name="href"><xsl:value-of select="@src"/></xsl:attribute>

在XSLT的开头(就在<xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> 之后)。

然后:

  • 由于xsl:stylesheet,您将获得格式正确的输出
  • 由于indent="yes"而不是strip-space,您将获得<fig> </fig>
相关问题