manipulation XML file with stylesheet. change attributes value depending on referenced items

36 Views Asked by At

I got a problem using a xml-stylesheet. Hope, a stylesheet can fullfill my requirements. Using a stylesheet I want to manipulate an XML-File in various ways. Everything works fine but one requirement. The XML-file is like this

<?xml version="1.0" encoding="UTF-8"?>
<Top>
    <Data id="id1" type="defaultType" />
    <DataVersion id="id2" type="otherType"  ObjRef="#id1"/>
    <DataVersion id="id6" type="yetAnotherType"/>
    <Graph>
        <Occ id="id3" insRef="#id2" oRefs="id4 id5"/>
        <Occ id="id4" insRef="#id6"/>
    </Graph>
</Top>

I want to change the "type" of the element "Data". The new type depends on a referenced elements type which is quite an issue to read. The way to find the type of the referenced element is like this:

  1. find "DataVersion" with attribute "ObjRef" with the value of the attribut "id" of the element "Data". You will find the DataVersion-element with "id=id2"
  2. read the "Occ"-Element which has attribut "insRef" which equals the value of "id" of "DataVersion" ( id2 ) with a leading hastag. You will find "Occ" with "id=id3"
  3. read the first id of attribut "oRefs" from Occ in step 3. You will find "id4"
  4. read "Occ" in which "id" equals the id from step 3. You will find Occ with id4
  5. read "insRef" of Occ with id4 without hastag
  6. read element "DataVersion" where attribut id equals the value of step 5. You will find Dataversion id="id6"
  7. read "type" of element from step 6 and make decision based on the type
  8. if type equals "yetAnotherType" write "YET" else write "customType"

here is my first try to solve this issue which is not very succesfull I guess. Maybe you got a hint how to handle this issue and if a stylesheet is the proper way of handling this.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Data/@type">
        <xsl:attribute name="type">

            <!-- not complete but first try -->
            <xsl:when test="//Occ[insRef='#//DataVersion[ObjRef='#..[id]'']@oRefs[1]] = 'yetAnotherType'">
                <xsl:value-of select="'YET'"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="'customType'"/>
            </xsl:otherwise>

        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>
0

There are 0 best solutions below