I have an xml document that looks like this:
<oldEle userlabel="label1">
<ele1>%02d.jpeg</ele1>
</oldEle>
<oldEle userlabel="label2">
<ele1>%02d.tiff</ele1>
</oldEle>
I want it to be this:
<JPEG userlabel="label1">
<ele1>%02d.jpeg</ele1>
</JPEG>
<TIFF userlabel="label2">
<ele1>%02d.tiff</ele1>
</TIFF>
I've tried this.
<xsl:template match="//xmlns:oldNode[contains(//xmlsns:oldNode/root:ele1, '.')]">
<xsl:element name="{translate(substring-after(//xmlns:ele1, '.'),
'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
but only get the first of the file ext. e.g. if jpeg is first, I would get for both of the nodes. Could someone offer expertise advice on why this is not working.
BTW, I also tried this but same thing happened:
<xsl:template match="//xmlns:oldNode[contains(//root:ele1, '.jpeg')]">
<xsl:element name="JPEG">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="//xmlns:oldNode[contains(//root:ele1, '.tiff')]">
<xsl:element name="TIFF">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
Here is the same answer as to your previous question -- this solves completely the old and the current problem and no recursion is used:
When this transformation is applied on the provided XML document, the wanted, correct result is produced:
Explanation:
In this transformation we are using the following XPath 1.0 expression to implement the standard XPath 2.0
ends-with($t, $suf)
function:The first problem is with your matching template
In particular, with the contains element you probably don't want the
//oldNode
at the front, as this will start looking for the first oldNode relative to the root element. What you really want is to look for the ele1 element relative to the element you have currently matched(I am not sure if you mean oldNode or oldEle by the way. I am also not sure where your namespaces fit in, so I have not shown them here).
The second problem is with the xsl:element, as you are doing a similar thing here
Because of the
//
in the substring-after, it will pick up the first ele1 relative to the root element of the XML, and not the one relative to your current element. You probably need to do thisTry this template instead
Similarly, for you second set of templates, you should be doing something like this