当我们在xslt中使用规范化空间时,子元素/标记会自动删除。如果我不想删除特定的子标记,那么我应该使用什么?
代码:
成人和青少年抗逆转录病毒指南小组。艾滋病毒感染成人和青少年使用抗逆转录病毒药物指南。美国卫生和公共服务部(DHHS);2014年5月定期更新。URL
XSLT代码:
<xsl:template match = "mixed-citation">
<xsl:element name = "p">
<xsl:value-of select="normalize-space()"/>
</xsl:element>
</xsl:template>在上面的代码中,我希望打印所有文本值,并删除除< uri>标记之外的所有标记。救命啊!
发布于 2018-06-28 07:14:34
如果您想跳过子代元素并复制某个特定的元素,那么您有两个选项,使用xsl:mode on-no-match="shallow-skip"作为默认值,然后为要复制的uri元素编写一个模板:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:mode on-no-match="shallow-skip"/>
<xsl:template match="mixed-citation">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="mixed-citation//text()">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
<xsl:template match="mixed-citation//uri">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>https://xsltfiddle.liberty-development.net/eiZQaFi
或者使用shallow-copy作为默认值,然后确保为descendants (而不是uri )覆盖它。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="mixed-citation">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="mixed-citation//text()">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
<xsl:template match="mixed-citation//*[not(self::uri)]">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>https://xsltfiddle.liberty-development.net/eiZQaFi/1
如果您使用的是早期版本,那么当前的XSLT版本3将看到https://www.w3.org/TR/xslt-30/#built-in-templates-shallow-skip如何将使用的xsl:mode声明转换为模板,例如,而不是
<xsl:mode on-no-match="shallow-skip"/>您可以使用
<xsl:template match="*"><xsl:apply-templates/></xsl:template>shallow-copy转换成众所周知的身份转换模板。
https://stackoverflow.com/questions/51076157
复制相似问题