XSLT 1.0移除子节点并保留格式的优化实现问询
XSLT 1.0 优化处理嵌套标签输出需求
原始XML文档
<doc> <article> <photo> <caption> <p> caption text here with <i>italic</i> here and <b>bold</b> here<credit>photo copyright</credit> </p> </caption> </photo> </article> </doc>
其中<credit>标签嵌套在<p>标签内。
已有模板及输出需求
已编写模板用于复制<i>和<b>标签:
<xsl:template match="i"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="b"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template>
需要实现的输出要求:
- 保留
<i>和<b>标签的标题文本 - 换行后输出图片版权信息
- 版权信息不重复
预期输出:
caption text here with <i>italic</i> here and <b>bold</b> here photo copyright
尝试过的写法及问题
- 写法1:丢失
<i>和<b>标签,且无换行
<xsl:apply-templates select="caption/p/text()"/> <xsl:value-of select="caption/p/credit"/>
- 写法2:版权信息重复,且无换行
<xsl:apply-templates select="caption/p"/> <xsl:value-of select="caption/p/credit"/>
当前实现及优化诉求
目前通过保留写法2并新增匹配<credit>的模板输出换行实现需求,但感觉较为繁琐。当前完整XSLT代码如下:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/doc"> <doc> <article> <xsl:for-each select="article/photo"> <xsl:apply-templates select="caption/p"/> <xsl:value-of select="caption/p/credit"/> </xsl:for-each> </article> </doc> </xsl:template> <xsl:template match="credit"> <xsl:text>
</xsl:text> </xsl:template> <xsl:template match="i"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="b"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="i[../../b]|b[../../i]"> <xsl:element name="emstrong"> <xsl:apply-templates/> </xsl:element> </xsl:template> </xsl:stylesheet>
寻求更优的XSLT 1.0实现方式。
内容的提问来源于stack exchange,提问作者Seb Mainguet
相关产品推荐
相关产品推荐

