首页 > 解决方案 > XML XSLT 删除节点中的空间

问题描述

我正在研究 xml/xslt 中的一个学校项目。我正在对 xml 文件进行转换。

在我的 xml 文件中有一个标签,它以不同的方式出现:

这个 :

<item year="2016" scriptwriter="duval" artist="rouge">Calvin Wax</item>

和这个:

  <item year="2013" scriptwriter="ranouil" artist="jailloux billon">
    La dernière conquête
  </item>

我希望在我的文本中得到这个结果:Calvin Wax 发表于 2016 年,S,A,

但我有第二种方法:

La dernière conquète
发表于 2013 年,S,A,

有什么想法可以解决吗?这是我的 xslt 代码:

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <xsl:stylesheet version="2.0" id="p2"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="text" encoding="ISO-8859-1"/>
    <xsl:strip-space elements="*"/>
    <xsl:variable name="eol" select="'&#10;'" as="xsd:string"/>
    <xsl:variable name="alinea" select="'&#160;&#160;&#160;&#160;'" as="xsd:string"/>

    <xsl:template match="comics">
        <xsl:apply-templates select ="collections"/>
    </xsl:template>

    <xsl:template match="collections">
       <xsl:apply-templates select ="series"/>
    </xsl:template>

    <xsl:template match="series">
        <xsl:value-of select="$eol"/>
        <xsl:text>     serie: </xsl:text>
        <xsl:value-of select="@name"/>
        <xsl:value-of select="$eol"/>
        <xsl:value-of select="$eol"/>
        <xsl:apply-templates select="item"/>

   </xsl:template>

   <xsl:template match="item">
        <xsl:value-of select="normalize-space(item)"/>
        <xsl:text>     </xsl:text>
        <xsl:apply-templates/>
        <xsl:text> published in </xsl:text>
        <xsl:value-of select="@year"/>
        <xsl:text>, </xsl:text>
        <xsl:if test="@additional='true'">
            <xsl:text>H, </xsl:text>
        </xsl:if>
        <xsl:choose>
             <xsl:when test="./@scriptwriter">
                 <xsl:text>S, </xsl:text>
             </xsl:when>
        </xsl:choose>
        <xsl:choose>
            <xsl:when test="./@artist">
                <xsl:text>A, </xsl:text>
            </xsl:when>
        </xsl:choose>
        <xsl:value-of select="$eol"/>
  </xsl:template>
  </xsl:stylesheet>

标签: xmlxslt

解决方案


你的第一表达

<xsl:value-of select="normalize-space(item)"/>

与所需节点不匹配text(),因为上下文项已经<item>由于模板的匹配规则。所以改成

<xsl:value-of select="normalize-space(.)"/>

并且(可能)删除以下内容<xsl:apply-templates/>(或将其更改为更具体的内容)。那么结果应该是

Calvin Wax      published in 2016, S, A, 
La dernière conquête      published in 2013, S, A,

推荐阅读