xml – 通过白名单元素清理SVG文档
发布时间:2020-12-16 07:45:26 所属栏目:百科 来源:网络整理
导读:我想从一些SVG文档中提取大约20种元素类型以形成新的SVG. 矩形,圆形,多边形,文本,折线,基本上是一组视觉部分都在白名单中. 需要使用 JavaScript,评论,动画和外部链接. 我想到了三种方法: 正则表达式:我完全熟悉,并且宁愿不去那里. PHP DOM:也许一年前使用
我想从一些SVG文档中提取大约20种元素类型以形成新的SVG.
矩形,圆形,多边形,文本,折线,基本上是一组视觉部分都在白名单中. 需要使用 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> 示例输出.显示完全相同的图像: <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> 守护者元素的近似列表是:g,rect,circle,ellipse,line,polyline,polygon,path,text,tspan,tref,textpath,linearGradient stop,radialGradient,defs,clippath,path. 如果没有专门的SVG微小,那么肯定是SVG lite.
Dimitre Novatchev的解决方案更加“干净”和优雅,但如果您需要“白名单”解决方案(因为您无法预测用户可能输入的内容,您需要“黑名单”),那么您需要完全充??实“白名单”.
<?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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |