xml - 使用 XSLT 删除双引号
问题描述
如果直接跟在 element 后面,我必须删除双引号<remove>some text</remove>
。例如
这要按照“一些植物”之类的东西来解释为“关于动词”
应该
这要按照一些植物等的东西来解释。
<p>This is to be interpreted in "<remove>about verb</remove>" accordance with something "<remove>some of plants</remove>" and so on</p>
应该
<p>This is to be interpreted in <remove>about verb</remove> accordance with something <remove>some of plants</remove> and so on</p>
解决方案
在XSLT 1.0中,这可以通过以下方式处理:
<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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:variable name="s" select="preceding-sibling::node()[1][self::remove] and starts-with(., '"')"/>
<xsl:variable name="e" select="following-sibling::node()[1][self::remove] and substring(., string-length(.))='"'"/>
<xsl:value-of select="substring(., 1 + $s, string-length(.) -$s -$e)"/>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- homebrew - GitHub Actions:自制程序慢
- php - 可见性修改器
- hyperledger-fabric - Hyperledger Fabric Metric 用于与各个对等方进行对等通信
- google-apps-script - Copy Spreadsheet to a new file, specify tabs
- unix - 是否可以从 find 命令输出中删除根目录?
- linux-kernel - 使用 eBPF 更改进程的 CPU 亲和性
- javascript - react native中全局变量的动态变化
- node.js - Node.js:同步 HTTP 端点
- javascript - 将 JavaScript 添加到没有页脚或页眉的 WordPress 网站
- python - 使用完成回调处理在单独进程中运行的异步作业中的中断