XSLT: Modify element name and attribute values
I'm completely new at this and am trying to figure out how to do something like this:
I've got this type of tags (lots of them) in an XML-file
<ImageData src="whatever.tif"/>
What I need to do is first change them to a reference with a number like this:
<INCL.ELEMENT FILEREF="image0001.tif" TYPE="TIFF"/>
so the number has to get leading zeros and the type has to be found in the src-attribute.
When all this has changed a list of these elements ALSO has to be added on top of the xml like this
<INCLUSIONS>
<INCL.ELEMENT FILEREF="image0001.tif" TYPE="TIFF"/>
<INCL.ELEMENT FILEREF="image0002.tif" TYPE="TIFF"/>
<INCL.ELEMENT FILEREF="image0003.tif" TYPE="TIFF"/>
<INCL.ELEMENT FILEREF="image0004.tif" TYPE="TIFF"/>
...
<INCL.ELEMENT FILEREF="image0014.tif" TYPE="TIFF"/>
</INCLUSIONS>
Since I'm new at this I'm clueless as to where to start开发者_运维百科.
This should give you something to start with:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="/">
<INCLUSIONS>
<xsl:apply-templates />
</INCLUSIONS>
</xsl:template>
<xsl:template match="ImageData">
<xsl:variable name="imagecount" select="count(preceding::ImageData) + 1" />
<xsl:variable name="fileextension" select="substring-after(./@src, '.')"/>
<INCL.ELEMENT>
<xsl:attribute name="FILEREF">
<xsl:value-of select="concat('image', format-number($imagecount, '0000'), '.', $fileextension)"/>
</xsl:attribute>
<xsl:attribute name="TYPE">
<xsl:choose>
<xsl:when test="$fileextension='tif'">TIFF</xsl:when>
<xsl:otherwise>JPEG</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</INCL.ELEMENT>
</xsl:template>
精彩评论