首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >xslt中的规范化空间

xslt中的规范化空间
EN

Stack Overflow用户
提问于 2018-06-28 06:34:35
回答 1查看 1.1K关注 0票数 1

当我们在xslt中使用规范化空间时,子元素/标记会自动删除。如果我不想删除特定的子标记,那么我应该使用什么?

代码:

成人和青少年抗逆转录病毒指南小组。艾滋病毒感染成人和青少年使用抗逆转录病毒药物指南。美国卫生和公共服务部(DHHS);2014年5月定期更新。URL

XSLT代码:

代码语言:javascript
运行
复制
<xsl:template match = "mixed-citation">
<xsl:element name = "p">
<xsl:value-of select="normalize-space()"/>
</xsl:element>
</xsl:template>

在上面的代码中,我希望打印所有文本值,并删除除< uri>标记之外的所有标记。救命啊!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-28 07:14:34

如果您想跳过子代元素并复制某个特定的元素,那么您有两个选项,使用xsl:mode on-no-match="shallow-skip"作为默认值,然后为要复制的uri元素编写一个模板:

代码语言:javascript
运行
复制
<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 )覆盖它。

代码语言:javascript
运行
复制
<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声明转换为模板,例如,而不是

代码语言:javascript
运行
复制
  <xsl:mode on-no-match="shallow-skip"/>

您可以使用

代码语言:javascript
运行
复制
  <xsl:template match="*"><xsl:apply-templates/></xsl:template>

shallow-copy转换成众所周知的身份转换模板。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51076157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档