I use the below xslt to transform xmls, it iterates thru the Receipt/Process
nodes and adds type attributes to the nodes. Based on the node’s value it will be string/int/float.
That part works fine.
I also should rename the “Node” node to “Node” concatenated with its index and
add an attribute which should be the “Name” node’s value.
I may have multiple “Nodes” which I would like to convert to something like this:
first Node to where the “Cuda3DLut” is coming from the <Name>Cuda3DLut</Name>
<Node1 type="Cuda3DLut" >
<Input type="ToolLink" role="Input" format="red,green,blue">Source</Input>
<Lut type="string">Identity</Lut>
<Output type="ToolLink" role="Output" format="red,green,blue">RESULT1</Output>
<bypass type="int">0</bypass>
<nodeRole type="string">viewing</nodeRole>
<nodeShortName type="string">LUT</nodeShortName>
</Node1>
second Node to
<Node2 type="CudaTool" >
...
</Node2>
I also would like to change the “Input” node’s value if it is “MainMedia” to “Source” but only then.
Thanks a lot.
Source XML:
<Receipt>
<Process>
<Node>
<Name>Cuda3DLut</Name>
<Input>MainMedia</Input>
<Lut>Identity</Lut>
<Output>RESULT1</Output>
<bypass>0</bypass>
<nodeRole>viewing</nodeRole>
<nodeShortName>LUT</nodeShortName>
</Node>
<Node>
<Name>CudaTool</Name>
<Input>MainMedia</Input>
<Lut>Identity</Lut>
<Output>RESULT1</Output>
<bypass>0</bypass>
<nodeRole>viewing</nodeRole>
<nodeShortName>LUT</nodeShortName>
</Node>
</Process>
<Encode>
<Job>
...
</Job>
</Encode>
</Receipt>
xslt:
<?xml version="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:template match="/">
<cut>
<xsl:apply-templates/>
</cut>
</xsl:template>
<xsl:template match="Process">
<xsl:for-each select="Node/*">
<xsl:choose>
<xsl:when test="name() = 'Input'">
<xsl:copy>
<xsl:attribute name="type">ToolLink</xsl:attribute>
<xsl:attribute name="role">Input</xsl:attribute>
<xsl:attribute name="format">red,green,blue</xsl:attribute>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:when>
<xsl:when test="name() = 'Output'">
<xsl:copy>
<xsl:attribute name="type">ToolLink</xsl:attribute>
<xsl:attribute name="role">Output</xsl:attribute>
<xsl:attribute name="format">red,green,blue</xsl:attribute>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<!-- Add type attribute to the node based on its value -->
<xsl:choose>
<xsl:when test="number(.) = .">
<xsl:choose>
<xsl:when test="contains(., '.')">
<xsl:copy>
<xsl:attribute name="type">float</xsl:attribute>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:attribute name="type">int</xsl:attribute>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:attribute name="type">string</xsl:attribute>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="Encode">
</xsl:template>
</xsl:stylesheet>
This transformation:
when applied on the provided XML document:
produces the wanted, correct result: