0

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.

1
  • Some feedback on my answer would have been nice. Commented Mar 21, 2010 at 20:23

1 Answer 1

2

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>

Sign up to request clarification or add additional context in comments.

4 Comments

substring-after(@src, '.') is somewhat brittle. substring(@src, string-length(@src) - 3, 3) is better.
Hhhmm ... but that would fail .jpeg and the like.
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.
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 ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.