tags:

views:

92

answers:

1

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.

+2  A: 

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"&gt;
    <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>

Filburt
`substring-after(@src, '.')` is somewhat brittle. `substring(@src, string-length(@src) - 3, 3)` is better.
Tomalak
Hhhmm ... but that would fail .jpeg and the like.
Filburt
Not as long as you are only interested in `'tif'`, as the question seems to indicate. Also, you could still compare to `'jpg'` and `'peg'`. Chances are quite small that an image file ends with `'peg'` and is *not* a jpeg.
Tomalak
Okay - if i'd strictly go for 'tif' i'd skip the substring altogether and hardcode the filename and the type attribute.... and one day for sure there's popping up an .mpeg ;-)
Filburt