I am trying to transform an xml from one format to another and i need some help as i am new to xslt. My input/source xml is like this:
<field id="1" media="video" name="ContentLayout" value="Refer to Content Layout View">
<specifiedLayout />
<actualLayout>
<segment endsmptetimecode="00:00:01:03" endtimecode="00:00:01:126" startsmptetimecode="00:00:00:00" starttimecode="00:00:00:000" type="Black" />
<segment endsmptetimecode="00:03:23:03" endtimecode="00:03:23:120" startsmptetimecode="00:00:01:03" starttimecode="00:00:01:126" type="Content" />
Goes on...
</actualLayout>
</field>
And the desired output xml should be like this:
<field id="1" media="video" name="ContentLayout" value="Refer to Content Layout View">
<specifiedLayout/>
<actualLayout>
<segment end="00:00:02:002" endSMPTE="00:00:02:00" start="00:00:00:000" startSMPTE="00:00:00:00" type="Black"/>
<segment end="00:00:47:081" endSMPTE="00:00:47:02" start="00:00:02:002" startSMPTE="00:00:02:00" type="Content"/>
</actualLayout>
</field>
And the mapping of the attributes is like this:
start = starttimecode
end = endtimecode
startSMPTE = startsmptetimecode
endSMPTE = endsmptetimecode
type = type
I am able to identify the xml node like this:
<xsl:when test="@name='ContentLayout'">
I am thinking of using a xsl:for-each
, and inside the loop declare 5 variables to store the attribute values, and then reassign it.
I some how feel its not right approach to do it. I dont know how can i use xsl:template
in this type of scenario.
Can anyone help me / advice ?
You can use local-name() to get the value of element/attribute. ex :
you try below code
In a case like this, where you need to keep most of the existing structure and only make a few changes, it is convenient to start with the identity transform template as the rule, and add a few templates as exceptions to the rule:
XSLT 1.0