I’ve been given the task to look into using xsl to update a few of our xml documents at work, and I have been looking at some tutorials for xsl, though I have yet to come across what I would ideally be looking for…
Since im not at work, wheres a small example of heres what im looking for:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<id>627</id>
<name>application1</name>
<url>www.application.com</url>
</application>
I would need to convert this to:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<id>627</id>
<application_name>application1</application_name>
<url>www.application.com</url>
</application>
Now from the examples and tutorials I’ve seen I could do this with a hard coded xsl sheet looking like so:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<application>
<xsl:apply-templates/>
</application>
</xsl:template>
<xsl:template match="id">
<id>
<xsl:value-of select="id"/>
<xsl:apply-templates/>
</id>
</xsl:template>
<xsl:template match="name">
<application_name>
<xsl:value-of select="name"/>
<xsl:apply-templates/>
</application_name>
</xsl:template>
<xsl:template match="url">
<url>
<xsl:value-of select="url"/>
<xsl:apply-templates/>
</url>
</xsl:template>
</xsl:stylesheet>
But this really wouldnt be practical since we have near 50 different xml documents that might need changing, and so I was really looking for a catch all template that I can use, and then only override the appropriate element that needs to be changed.
Does this functionality exist in xsl?
Let me see if I understand what you need. The first template rule in the transform below is called identity transform processes all nodes by copying them, and can be overridden for individual elements, attributes, comments, processing instructions, or text nodes. In your case, I’ve just overridden for the element
name.XSLT 1.0 tested under Saxon 6.5.5
Applied on this input:
Produces: