IMPORTANT:
this is not a Support Forum! Experienced users might answer from time to time questions posted here. If you need a professional and reliable answer, or if you want to report a bug, please contact Altova Support instead.

XSLT select each node and wrapping it Options · View
sankeyr
Posted: Friday, May 26, 2017 10:53:47 PM
Rank: Newbie

Joined: 5/26/2017
Posts: 1
Location: Wyoming
Here is the XML:

Code:
  <svg xmlns:xlink="http://www.w3.org/1999/xlink">
        <g>
            <text id="b376">
                <tspan x="59" y="156" font-size="13px" font-family="Arial">80</tspan>
            </text>
            <use xlink:href="#b376" fill="#000000"/>
            <text id="b374">
                <tspan x="59" y="204" font-size="13px" font-family="Arial">60</tspan>
            </text>
            <use xlink:href="#b374" fill="#000000"/>
            <defs>testDef</defs>
    </g>
    </svg>


Here is my XSL input:

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="yes"/>
      <xsl:strip-space elements="*"/>
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
      <xsl:template match="g">
        <g>
          <xsl:apply-templates select="use|defs"/>
          <defs>
            <xsl:apply-templates select="*[name() != 'use' and name() != 'defs']"/>
          </defs>
        </g>
      </xsl:template>
    </xsl:stylesheet>


I want to wrap all nodes in a <defs> tag EXCEPT <use> tags and <defs> tags. So the 2 <text> nodes would be wrapped in <defs> tags but <defs> and <use> would not.

Here is what I am getting

Code:
<?xml version="1.0"?>
    <svg xmlns:xlink="http://www.w3.org/1999/xlink">
      <g>
        <use xlink:href="#b376" fill="#000000"/>
        <use xlink:href="#b374" fill="#000000"/>
        <defs>testDef</defs>
        <defs>
          <text id="b376">
            <tspan x="59" y="156" font-size="13px" font-family="Arial">80</tspan>
          </text>
          <text id="b374">
            <tspan x="59" y="204" font-size="13px" font-family="Arial">60</tspan>
          </text>
        </defs>
      </g>
    </svg>


This is what I want:

Code:
<?xml version="1.0"?>
        <svg xmlns:xlink="http://www.w3.org/1999/xlink">
          <g>
            <use xlink:href="#b376" fill="#000000"/>
            <use xlink:href="#b374" fill="#000000"/>
            <defs>testDef</defs>
            <defs>
              <text id="b376">
                <tspan x="59" y="156" font-size="13px" font-family="Arial">80</tspan>
              </text>
            </defs>
             <defs>
              <text id="b374">
                <tspan x="59" y="204" font-size="13px" font-family="Arial">60</tspan>
              </text>
            </defs>
          </g>
        </svg>


Thanks!
Users browsing this topic
guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Use of the Altova User Forum(s) is governed by the Altova Terms of Use.