XSLT help

Hi, searched but cannot find any relevant thread.

I have some xml in one format which needs transforming into another, and have been told the best way to do this is via xslt. I don’t know xslt, have tried tutorials etc but it beyond me. (I am not sure that it can even do what I want it to do).

I have xml which I need to import, but it has a different structure to the xml we use so I need to convert it. In the xml I need to convert, only primary terms (pts) have an ID but I need everything to have an ID. (Pts and non descriptors (nds) I also need these IDs inserted into heirachies - NT are children of terms.

The code I have is like this



<?xml version="1.0" encoding="utf-8"?>
<zthes>
    <term>
        <termName>term1</termName>
        <termType>Nd</termType>
      
        <relation>
            <relationType>USE</relationType>
            <termName>term2</termName>
        </relation>
    </term>

    <term>
        <termName>term2</termName>
        <termType>Pt</termType>
        <termId>
            386639001
        </termId>
        <relation>
            <relationType>NT</relationType>
            <termName> term3</termName>
        </relation>
        </term>

     <term>
        <termName>term3</termName>
        <termType>Pt</termType>
   <termId>
            386639002
        </termId>
</term>
      </zthes>


I need to transform it into the following structure:



<?xml version="1.0" encoding="utf-8"?>
<zthes>
    <term>
        <termName>term1</termName>
[B]<termId>INSERT INCREMENTAL NUMBER FROM 1 here</termId>[/B]
        <termType>Nd</termType>
      
        <relation>
            <relationType>USE</relationType>
            <termName>term2</termName>
[B]<termId>look up term2 ID and insert it here</termId>[/B]
        </relation>
    </term>

    <term>
        <termName>term2</termName>
        <termType>Pt</termType>
        <termId>
            386639001
        </termId>
        <relation>
            <relationType>NT</relationType>
            <termName> term3</termName>

[B]<termId>look up term 3 id and insert it here</termId>[/B]
        </relation>
        </term>
</zthes>

If someone could a) let me know whether this is possible via xslt b) let me know what the functions xslt uses for this (so I can do a better search on the forums and see if I can figure out how to do it) or c) give me some more pointers, that would be incredibly appreciated.

many thanks

I’d write a rule which matches the root, then use a for-each loop to iterate over the terms:

<xsl:for-each select="term">

You can get an incremental number using the position function.

<xsl:value-of select="position()" />

Then, if your relationType is always NT, just use position() + 1 to get the correct id.

Good luck with it! :slight_smile: