xml - 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="' '" as="xsd:string"/>
<xsl:variable name="alinea" select="'    '" 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>
解决方案
你的第一表达
<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,
推荐阅读
- node.js - Selenium 测试用例失败,“脚本返回退出代码 255”
- c++ - 为什么 Cplex 提供了一个约束松弛的解决方案?
- python - 在 Airflow 中的组件之间传输数据
- javascript - 在 Select 上更改 event.target.value 类型
- ios - AppAuth - 外部浏览器窗口不会打开?
- python - 如何将具有两个列表的字典合并为值?
- grails - 如果我知道剩余时间,我如何计算 Grails 中的到期日期?
- hash - “overpass-the-hash”中的“over”是什么意思?
- groovy - 如何从 Atlassian 网站解析 jira 版本
- ios - DAYLIGHT SAVING TIME 中的月末日期问题