0

I want to remove the <a> tags in the following, but keep the children (to remove the hotspots):

 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <title>GRAPHICS_000</title>
  <g id="C3DB0001" name="Background" type="layer"/>
  <g id="C3DG0001" name="Drawing" type="layer">
    <g transform="matrix(3.78095,0,0,3.78095,-0,1122.97)" stroke-width="0.35">
      <path stroke="#000" d="M148.47,-204.86 C148.29,-204.83"/>
      <path stroke="#000" stroke-width="0.18" d="M148.38,-203.2 "/>
    </g>
  </g>
  <g id="C3DA0001" name="Axials" type="layer"/>
      <g id="C3DC0001" name="Callouts" type="layer">
        <a id="e884b23a2e34327fba7db5424d88ee54" name="20" type="grobject" xlink:href="#">
          <path d="M431.06,269.22 L454.55,269.22 454.55,285.35 431.06,285.35 Z" fill="#fff" fill-opacity="0"/>
          <g transform="matrix(3.78095,0,0,3.78095,-0,1122.97)" stroke-linejoin="miter">
            <path stroke="#000" d="M117.12,-221.54 L121.44,-197.39"/>
            <text fill="#000">20</text>
          </g>
        </a>
        <a id="e7dc1641ea0314fed4e538136162aa6b.1" name="30" type="grobject" xlink:href="#">
          <path d="M338.24,916 L361.73,916 361.73,932.13 338.24,932.13 Z" fill="#fff" fill-opacity="0"/>
          <g transform="matrix(3.78095,0,0,3.78095,-0,1122.97)" stroke-width="0.18">
            <path stroke="#000" d="M89.458,-54.741 L82.214,-82.997"/>
            <text fill="#000">30</text>
          </g>
        </a>
      </g>
  <g id="C3DP0001" name="Panels" type="layer"/>
</svg>

Using Ant and <replaceregexp>

<replaceregexp match="&lt;a[\s].*>|&lt;/a>" replace="" flags="gi">
   <fileset dir="${dir}"/>
</replaceregexp>

did remove the <a> tags but left unwanted blank lines:

<g id="C3DA0001" name="Axials" type="layer"/>
      <g id="C3DC0001" name="Callouts" type="layer">
        
          <path d="M431.06,269.22 L454.55,269.22 454.55,285.35 431.06,285.35 Z" fill="#fff" fill-opacity="0"/>
          <g transform="matrix(3.78095,0,0,3.78095,-0,1122.97)" stroke-linejoin="miter">
            <path stroke="#000" d="M117.12,-221.54 L121.44,-197.39"/>
            <text fill="#000">20</text>
          </g>
        
        
          <path d="M338.24,916 L361.73,916 361.73,932.13 338.24,932.13 Z" fill="#fff" fill-opacity="0"/>
          <g transform="matrix(3.78095,0,0,3.78095,-0,1122.97)" stroke-width="0.18">
            <path stroke="#000" d="M89.458,-54.741 L82.214,-82.997"/>
            <text fill="#000">30</text>
          </g>
        
      </g>
  <g id="C3DP0001" name="Panels" type="layer"/>

By looking at this question

I changed it to:

<replaceregexp match="(\r?\n).*?&lt;a[\s].*>\r?\n" replace="\1" flags="gi">
   <fileset dir="${dir}"/>
</replaceregexp>
<replaceregexp match="(\r?\n).*?&lt;/a>\r?\n" replace="\1" flags="gi">
  <fileset dir="${dir}"/>
</replaceregexp>

Which works, but surely there is a better way, as this is clunky and slow. I thought ANT and regex would be faster than XSLT 1.0, my other alternative.

Caroline
  • 167
  • 1
  • 11
  • 1
    SVG is XML which is not REGULAR and REGEX (Regular Expressions) should not be used. Try an XML library instead. Just because another posting used the wrong method doesn't mean you should make the same mistake. – jdweng Feb 28 '23 at 07:17
  • 1
    XSLT is the answer I imagine. See https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Robert Longson Feb 28 '23 at 07:36
  • @RobertLongson that scary link has set me straight. – Caroline Feb 28 '23 at 15:20

1 Answers1

0

I'm glad I posted this question because I learned a lot. I originally tried to solve with XSLT. After reviewing similar questions (again) I realized my mistake was not including the svg namespace.

<xsl:stylesheet version="1.0" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
   <xsl:strip-space elements="*"/>
   <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
   
   <xsl:template match="/">
       <xsl:text disable-output-escaping="yes">&#xA;<![CDATA[<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">]]>&#xA;</xsl:text>
      <xsl:apply-templates />
   </xsl:template>
          
    <xsl:template match="svg:a">
        <xsl:apply-templates select="*"/>
    </xsl:template>
    
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
   </xsl:template>

</xsl:stylesheet>
Caroline
  • 167
  • 1
  • 11