Home > Software engineering >  from XML to XSLT transforming Tags
from XML to XSLT transforming Tags

Time:12-09

I would like to transform this file to xml format using xslt

Our initial xml file. I want to see the output without any values. But when I use this code, the output contain exist stuff that I really don't care. My goal is find matching beetween 'Exercise1' and firstExercise. So, when we find Exercise1 it should be transformated to firstExercise and so on.

<?xml version="1.0" encoding="UTF-8"?>
<option>
    <Exercise1>
        <ExerciseTime>
            <hourMinuteTime>00:00:00</hourMinuteTime>
        </ExerciseTime>
    </Exercise1>
    <Exercise2>
        <ExerciseTime>
            <hourMinuteTime>00:00:00</hourMinuteTime>
        </ExerciseTime>
    </Exercise2>
    <Exercise3>
        <ExerciseTime>
            <hourMinuteTime>00:00:00</hourMinuteTime>
        </ExerciseTime>
    </Exercise3>
</option>

CodePudding user response:

Well, XSLT 3 can do that with e.g.

  <xsl:template match="*[matches(local-name(), '^Exercise[0-9] $')]">
    <xsl:element name="{format-integer(xs:integer(substring-after(local-name(), 'Exercise')), 'w;o', 'en')}Excercise">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

I think, use <xsl:mode on-no-match="shallow-copy"/> to ensure the other stuff is copied through.

  • Related