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 ?
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
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@starttimecode">
<xsl:attribute name="start">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@endtimecode">
<xsl:attribute name="end">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@startsmptetimecode">
<xsl:attribute name="startSMPTE">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@endsmptetimecode">
<xsl:attribute name="endSMPTE">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
You can use local-name() to get the value of element/attribute.
ex :
you try below code
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@starttimecode|@endtimecode|@startsmptetimecode|@endsmptetimecode|@type">
<xsl:choose>
<xsl:when test="local-name() = 'starttimecode'">
<xsl:attribute name="start">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:when>
<xsl:when test="local-name() = 'endtimecode'">
<xsl:attribute name="end">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:when>
<xsl:when test="local-name() = 'startsmptetimecode'">
<xsl:attribute name="startSMPTE">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:when>
<xsl:when test="local-name() = 'endsmptetimecode'">
<xsl:attribute name="endSMPTE">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:when>
<xsl:when test="local-name() = 'type'">
<xsl:attribute name="type">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:when>
</xsl:choose>
</xsl:template>