Sam*_*amG 7 xml xslt svg xml-parsing
我想从一些SVG文档中提取大约20种元素类型,以形成一个新的SVG.
rect,circle,polygon,text,polyline,基本上是一组视觉部分的是在白名单.需要使用JavaScript,评论,动画和外部链接.
我想到了三种方法:
如果XSLT是适合该作业的工具,我需要什么xsl:stylesheet?否则,您会使用哪种方法?
输入示例:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1" width="512" height="512" id="svg2">
<title>Mostly harmless</title>
<metadata id="metadata7">Some metadata</metadata>
<script type="text/ecmascript">
<![CDATA[
alert('Hax!');
]]>
</script>
<style type="text/css">
<![CDATA[ svg{display:none} ]]>
</style>
<defs id="defs4">
<circle id="my_circle" cx="100" cy="50" r="40" fill="red"/>
</defs>
<g id="layer1">
<a xlink:href="www.hax.ru">
<use xlink:href="#my_circle" x="20" y="20"/>
<use xlink:href="#my_circle" x="100" y="50"/>
</a>
</g>
<text>
<tspan>It was the best of times</tspan>
<tspan dx="-140" dy="15">It was the worst of times.</tspan>
</text>
</svg>
Run Code Online (Sandbox Code Playgroud)
示例输出.显示完全相同的图像:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="512" height="512">
<defs>
<circle id="my_circle" cx="100" cy="50" r="40" fill="red"/>
</defs>
<g id="layer1">
<use xlink:href="#my_circle" x="20" y="20"/>
<use xlink:href="#my_circle" x="100" y="50"/>
</g>
<text>
<tspan>It was the best of times</tspan>
<tspan dx="-140" dy="15">It was the worst of times.</tspan>
</text>
</svg>
Run Code Online (Sandbox Code Playgroud)
守门员要素的近似列表是:g, rect, circle, ellipse, line, polyline, polygon, path, text, tspan, tref, textpath, linearGradient+stop, radialGradient, defs, clippath, path.
如果没有专门的SVG微小,那么肯定是SVG lite.
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://www.w3.org/2000/svg"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="namespace::xlink"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name()}"
namespace="{namespace-uri()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="s:a">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match=
"s:title|s:metadata|s:script|s:style|
s:svg/@version|s:svg/@id"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当应用于提供的 XML 文档时:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg" version="1.1"
width="512" height="512" id="svg2">
<title>Mostly harmless</title>
<metadata id="metadata7">Some metadata</metadata>
<script type="text/ecmascript"><![CDATA[ alert('Hax!'); ]]></script>
<style type="text/css"><![CDATA[ svg{display:none} ]]></style>
<defs id="defs4">
<circle id="my_circle" cx="100" cy="50" r="40" fill="red"/>
</defs>
<g id="layer1">
<a xlink:href="www.hax.ru">
<use xlink:href="#my_circle" x="20" y="20"/>
<use xlink:href="#my_circle" x="100" y="50"/>
</a>
</g>
<text>
<tspan>It was the best of times</tspan>
<tspan dx="-140" dy="15">It was the worst of times.</tspan>
</text>
</svg>
Run Code Online (Sandbox Code Playgroud)
产生想要的、正确的结果:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="512" height="512">
<defs id="defs4">
<circle id="my_circle" cx="100" cy="50" r="40" fill="red"/>
</defs>
<g id="layer1">
<use xlink:href="#my_circle" x="20" y="20"/>
<use xlink:href="#my_circle" x="100" y="50"/>
</g>
<text>
<tspan>It was the best of times</tspan>
<tspan dx="-140" dy="15">It was the worst of times.</tspan>
</text>
</svg>
Run Code Online (Sandbox Code Playgroud)
说明:
两个模板,具有类似于身份规则的组合效果,匹配所有“白名单节点”,并基本上复制它们(仅消除不需要的命名空间节点)。
没有正文的模板匹配所有“列入黑名单”的节点(元素和一些属性)。这些被有效地删除。
必须有匹配特定“灰名单”节点的模板(s:a在我们的例子中是模板匹配)。“灰名单节点不会被完全删除——它可能会被重命名或以其他方式修改,或者至少它的内容可能仍然包含在输出中。
很可能随着你对问题的理解越来越清晰,三个列表会不断增长,所以黑名单删除模板的匹配模式会被修改,以适应新发现的黑名单元素。新发现的白名单节点根本不需要工作。仅处理新的灰名单元素(如果发现了此类元素)将需要更多的工作。
Dimitre Novaatchev 的解决方案更加“干净”和优雅,但是如果您需要一个“白名单”解决方案(因为您无法预测用户可能输入的内容需要“黑名单”),那么您需要充分充实“白名单”。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:svg="http://www.w3.org/2000/svg">
<xsl:output indent="yes" />
<!--The "whitelist" template that will copy matched nodes forward and apply-templates
for any attributes or child nodes -->
<xsl:template match="svg:svg
| svg:defs | svg:defs/text()
| svg:g | svg:g/text()
| svg:a | svg:a/text()
| svg:use | svg:use/text()
| svg:rect | svg:rect/text()
| svg:circle | svg:circle/text()
| svg:ellipse | svg:ellipse/text()
| svg:line | svg:line/text()
| svg:polyline | svg:polyline/text()
| svg:polygon | svg:polygon/text()
| svg:path | svg:path/text()
| svg:text | svg:text/text()
| svg:tspan | svg:tspan/text()
| svg:tref | svg:tref/text()
| svg:textpath | svg:textpath/text()
| svg:linearGradient | svg:linearGradient/text()
| svg:radialGradient | svg:radialGradient/text()
| svg:clippath | svg:clippath/text()
| svg:text | svg:text/text()">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<!--The "blacklist" template, which does nothing except apply templates for the
matched node's attributes and child nodes -->
<xsl:template match="@* | node()">
<xsl:apply-templates select="@* | node()" />
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1763 次 |
| 最近记录: |