FW: Outputting element name and position as element ID

 
I am transforming XML data to XML data and want to insert an ID for each
child element of a particular parent:
 
 
 
For example, I have the following XML data:
 
<Block ID="B_35000">
    <BlockLabel>Introduction</BlockLabel>
    <BlockContent>
 
        <Para>This is the RLO introduction</Para>
 
        <List>
            <ListTitle>LIST TITLE</ListTitle>
            <ListParagraphText>THi is th paragraph text prior to the
list: </ListParagraphText>
            <ListItem>
                <Para>This is the first list item</Para>
            </ListItem>
            <ListItem/>
            <ListItem>
                <Para>This is the second list item</Para>
            </ListItem>
        </List>
 
        <Para>This is the paragraph after the list</Para>
 
    </BlockContent>
</Block>
 
I would like to add in an ID for each Para and List element of the
BlockContent element, based on the Block elements ID. The requested
result is as follows:
 
<Block ID="B_35000">
    <BlockLabel>Introduction</BlockLabel>
    <BlockContent>
 
        <Para ID="B_35000_1">This is the RLO introduction</Para>
 
        <List ID="B_35000_2">
            <ListTitle>LIST TITLE</ListTitle>
            <ListParagraphText>THi is th paragraph text prior to the
list: </ListParagraphText>
            <ListItem>
                <Para>This is the first list item</Para>
            </ListItem>
            <ListItem/>
            <ListItem>
                <Para>This is the second list item</Para>
            </ListItem>
        </List>
 
        <Para ID="B_35000_3">This is the paragraph after the list</Para>
 
    </BlockContent>
</Block>
 
I am currently using the following XSL code, which obviously goes
through the Para elements first, then the List elements second, so it
destroys the original order:
 
 
<xsl:template match="Block">
<xsl:element name="Block">
<xsl:variable name="BLOCKID" select="@ID"></xsl:variable>
<xsl:attribute name="ID" >
<xsl:value-of select="$BLOCKID"/>
</xsl:attribute>
<xsl:apply-templates select="BlockLabel"></xsl:apply-templates>
 
<xsl:for-each select="BlockContent">
<xsl:element name="BlockContent">
<xsl:for-each select="Para">
<xsl:element name="Para">
<xsl:attribute name="ID">
<xsl:value-of select="$BLOCKID"></xsl:value-of>
<xsl:text>_</xsl:text>
<xsl:value-of select="position()"></xsl:value-of>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="List">
<xsl:element name="List">
<xsl:attribute name="ID">
<xsl:value-of select="$BLOCKID"></xsl:value-of>
<xsl:text>_</xsl:text>
<xsl:value-of select="position()"></xsl:value-of>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
 
</xsl:element>
</xsl:template>
 
How can I get the element name and write out the element name with the
appropriate attribute based on the $BLOCKID and position of the element,
and then obviously write out the element content? I think I did
something like this months ago, but can't seem to come up with it again.
Any help would be appreciated!
 
Thanks.
 
Teresa Rippeon
ManTech
teresa.rippeon@mantech-stc.com
 

Received on Thursday, 12 June 2003 09:05:15 UTC