xslt - xslt 中的规范化空间
问题描述
当我们在 xslt 中使用标准化空间时,子元素/标签会被自动删除。如果我不想删除特定的子标签,那么我应该使用什么?
代码:
<mixed-citation publication-type="book"> <collab>Panel on Antiretroviral Guidelines for Adults and Adolescents</collab> . <source> Guidelines for the use of antiretroviral agents in HIV-1-infected adults and adolescents </source> . <publisher-loc>Rockville, MD</publisher-loc> : <publisher-name>US Department of Health and Human Services (DHHS)</publisher-name>; May <year>2014</year> [regularly updated]. [ <uri xlink:href="http://aidsinfo.nih.gov/guidelines/html/1/adult-and-adolescent-arv-guidelines/0">URL</uri>] </mixed-citation> </ref>
XSLT 代码:
<xsl:template match = "mixed-citation">
<xsl:element name = "p">
<xsl:value-of select="normalize-space()"/>
</xsl:element>
</xsl:template>
在上面的代码中,我想打印所有文本值并删除除 <uri> 标签之外的所有标签。请帮忙 !!!
解决方案
如果您想跳过后代元素并复制某个元素,那么您有两个选择,使用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
如果您使用的是早期版本,那么当前版本 3 的 XSLT 然后请参阅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
转换为众所周知的身份转换模板。
推荐阅读
- python - 如何根据条件将数据框列中的值写入另一列?
- python - Flask-Admin Bootstrap 4 X-editable date 导致错误
- javascript - 在 React w/Redux 中映射后如何访问对象的道具?
- inheritance - Solidity 合约可以同时继承基类和接口吗?
- ansible - Ansible 任务条件总是错误地引用 dict 成员
- ios - 使用 SwiftUI 将 JSON 解析为 HStack
- spring - 无法通过数据库将数据获取到碧玉报告中
- powershell - Powershell - 重命名文件
- flutter - Flutter path_provider 获取 _CastError
- hgrc - 当我尝试“hg config --edit”命令时,HGRC 文件无法正常工作