Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3843042
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:52:52+00:00 2026-05-19T15:52:52+00:00

I want to extract around 20 element types from some SVG documents to form

  • 0

I want to extract around 20 element types from some SVG documents to form a new SVG.
rect, circle, polygon, text, polyline, basically a set of visual parts are in the white list.
JavaScript, comments, animations and external links need to go.

Three methods come to mind:

  1. Regex: I’m completely familiar with, and would rather not go there obviously.
  2. PHP DOM: Used once perhaps a year ago.
  3. XSLT: Took my first look just now.

If XSLT is the right tool for the job, what xsl:stylesheet do I need?
Otherwise, which approach would you use?

Example input:

<?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>

Example output. Displays exactly the same image:

<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>

The approximate list of keeper elements is: g, rect, circle, ellipse, line, polyline, polygon, path, text, tspan, tref, textpath, linearGradient+stop, radialGradient, defs, clippath, path.

If not specifically SVG tiny, then certainly SVG lite.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:52:53+00:00Added an answer on May 19, 2026 at 3:52 pm

    Dimitre Novatchev’s solution is more “clean” and elegant, but if you need a “whitelist” solution (because you can’t predict what content users may input that you would need to “blacklist”), then you would need to fully flesh out the “whitelist”.

    <?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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to extract the text from an HTML file using Python. I want
I want to extract an ID from a string which has the form somethinguseless_XXXXX
I want to extract text from a column using regular expressions in Oracle 11g.
I want to extract all links inside some pages with their link text to
I have barcode images in jpg format and want to extract barcode # from
Given a filename in the form someletters_12345_moreleters.ext , I want to extract the 5
I want to use Perl to extract information from a Certificate Signing Request ,
I want to extract information from three tables which are linked by certain ids
I want to extract the fields from a line into variables: aaa bbb ccc
I have a string (char) and I want to extract numbers out of it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.