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.

Profile: sankeyr
About
User Name: sankeyr
Forum Rank: Newbie
Real Name:
Location Wyoming
Occupation:
Interests:
Gender: None Specified
Statistics
Joined: Friday, May 26, 2017
Last Visit: Friday, May 26, 2017 10:53:48 PM
Number of Posts: 1
[0.01% of all post / 0.00 posts per day]
Avatar
Last 10 Posts
Topic: XSLT select each node and wrapping it
Posted: Friday, May 26, 2017 10:53:47 PM
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!

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