如何用 SVG 文件中的值替换所有出现的 XML 标记
How to replace all occurences of an XML tag by its value in an SVG file
如何使用 xslt 模板将给定 XML 标签 的所有出现替换为其值?
例如,<tspan x="12.02" y="0">ogen</tspan>
会变成 ogen
。
我可以使用此命令行删除所有出现的事件:
xmlstarlet ed -N ns=http://www.w3.org/2000/svg -d "//ns:tspan" foo.svg
但是我还是找不到用它的值代替它的方法。
此代码段将执行您想要的操作:
<xsl:template match="tspan">
<xsl:value-of select="text()"/>
</xsl:template>
它找到了 tspan
个元素,并丢弃了它们的所有内容。 xsl:value-of select="text()"
语句仅将文本节点的内容复制到输出。
考虑使用带有包含必要规则的模板的 XSL 样式表。例如:
条-tag.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="node()[not(name()='tspan')]|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
此模板匹配所有节点并复制它们。但是 match
属性中定义的 XPath 表达式(即 [not(name()='tspan')]
部分)排除了任何 tspan
元素节点及其关联的属性节点被复制 -有效地删除它们。 tspan
元素的子元素节点 and/or 文本节点将被复制,因此它们将根据需要保留在输出中。
source.xml
考虑以下示例 source.xml
文件:
<?xml version="1.0"?>
<svg width="250" height="40" viewBox="0 0 250 40" xmlns="http://www.w3.org/2000/svg" version="1.1">
<text x="10" y="10">The <tspan x="10" y="10">quick</tspan> brown fox <tspan x="30" y="30">jumps</tspan> over the lazy dog</text>
<a href="https://www.example.com"><text x="100" y="100"><tspan x="50" y="50">click</tspan> me</text></a>
</svg>
转换源xml
运行 以下 xmlstarlet
命令 (为文件定义了正确的路径):
$ xml tr path/to/strip-tag.xsl path/to/source.xml
或运行以下xsltproc
命令(如果您的系统可用):
$ xsltproc path/to/strip-tag.xsl path/to/source.xml
将向控制台打印以下内容:
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" width="250" height="40" viewBox="0 0 250 40" version="1.1">
<text x="10" y="10">The quick brown fox jumps over the lazy dog</text>
<a href="https://www.example.com"><text x="100" y="100">click me</text></a>
</svg>
注意: 开始和结束 tspan
标签的所有实例已被删除。
删除多个
要删除多个不同的命名元素,请在 match
属性中定义的 XPath 表达式中使用 and
运算符。例如:
<!-- strip-multiple-tags.xsl-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="node()[not(name()='tspan') and not(name()='a')]|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
使用此模板转换 source.xml
将产生以下输出:
<svg xmlns="http://www.w3.org/2000/svg" width="250" height="40" viewBox="0 0 250 40" version="1.1">
<text x="10" y="10">The quick brown fox jumps over the lazy dog</text>
<text x="100" y="100">click me</text>
</svg>
注意: tspan
和 a
标签的所有实例都已删除。
如何使用 xslt 模板将给定 XML 标签 的所有出现替换为其值?
例如,<tspan x="12.02" y="0">ogen</tspan>
会变成 ogen
。
我可以使用此命令行删除所有出现的事件:
xmlstarlet ed -N ns=http://www.w3.org/2000/svg -d "//ns:tspan" foo.svg
但是我还是找不到用它的值代替它的方法。
此代码段将执行您想要的操作:
<xsl:template match="tspan">
<xsl:value-of select="text()"/>
</xsl:template>
它找到了 tspan
个元素,并丢弃了它们的所有内容。 xsl:value-of select="text()"
语句仅将文本节点的内容复制到输出。
考虑使用带有包含必要规则的模板的 XSL 样式表。例如:
条-tag.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="node()[not(name()='tspan')]|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
此模板匹配所有节点并复制它们。但是 match
属性中定义的 XPath 表达式(即 [not(name()='tspan')]
部分)排除了任何 tspan
元素节点及其关联的属性节点被复制 -有效地删除它们。 tspan
元素的子元素节点 and/or 文本节点将被复制,因此它们将根据需要保留在输出中。
source.xml
考虑以下示例 source.xml
文件:
<?xml version="1.0"?>
<svg width="250" height="40" viewBox="0 0 250 40" xmlns="http://www.w3.org/2000/svg" version="1.1">
<text x="10" y="10">The <tspan x="10" y="10">quick</tspan> brown fox <tspan x="30" y="30">jumps</tspan> over the lazy dog</text>
<a href="https://www.example.com"><text x="100" y="100"><tspan x="50" y="50">click</tspan> me</text></a>
</svg>
转换源xml
运行 以下
xmlstarlet
命令 (为文件定义了正确的路径):$ xml tr path/to/strip-tag.xsl path/to/source.xml
或运行以下
xsltproc
命令(如果您的系统可用):$ xsltproc path/to/strip-tag.xsl path/to/source.xml
将向控制台打印以下内容:
<?xml version="1.0"?> <svg xmlns="http://www.w3.org/2000/svg" width="250" height="40" viewBox="0 0 250 40" version="1.1"> <text x="10" y="10">The quick brown fox jumps over the lazy dog</text> <a href="https://www.example.com"><text x="100" y="100">click me</text></a> </svg>
注意: 开始和结束 tspan
标签的所有实例已被删除。
删除多个
要删除多个不同的命名元素,请在 match
属性中定义的 XPath 表达式中使用 and
运算符。例如:
<!-- strip-multiple-tags.xsl-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="node()[not(name()='tspan') and not(name()='a')]|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
使用此模板转换 source.xml
将产生以下输出:
<svg xmlns="http://www.w3.org/2000/svg" width="250" height="40" viewBox="0 0 250 40" version="1.1"> <text x="10" y="10">The quick brown fox jumps over the lazy dog</text> <text x="100" y="100">click me</text> </svg>
注意: tspan
和 a
标签的所有实例都已删除。