XML XSLT transform to create relations Primary and Foreign keys

gochakv

New Member
I want to create relations between parent and child elements in xml using xslt (primary keys PK and foreign keys FK). The source xml look like this:\[code\]<root> <Patient> <Fname>John</Fname> <Lname>Doe</Lname> <Record>F00025</Record> <Disease> <Date>3/3/2009</Date> <Dcode>D0456</Dcode> <Comm>comment</Comm> <Medicine> <Mcode>M00025</Mcode> <Qnt>0.01</Qnt> <Unit>l</Unit> </Medicine> </Disease> <Disease> <Date>3/15/2009</Date> <Dcode>D4415</Dcode> <Comm></Comm> </Disease> <Disease> <Date>3/19/2009</Date> <Dcode>D0176</Dcode> <Comm></Comm> <Medicine> <Mcode>M00015</Mcode> <Qnt>10</Qnt> <Unit>ml</Unit> </Medicine> <Medicine> <Mcode>M00006</Mcode> <Qnt>1</Qnt> <Unit>m</Unit> <Icode> <Num>803125</Num> <Num>803005</Num> </Icode> </Medicine> </Disease> </Patient> <Patient> <Fname>Jayne</Fname> <Lname>Joyce</Lname> <Record>F00156</Record> <Disease> <Date>3/18/2009</Date> <Dcode>D3266</Dcode> <Comm></Comm> <Medicine> <Mcode>M00006</Mcode> <Qnt>1</Qnt> <Unit>m</Unit> <Icode> <Num>803125</Num> </Icode> </Medicine> </Disease> </Patient></root>\[/code\]The transformed XML should look like this one:\[code\]<root> <Patient> <Patient_PK>1</Patient_PK> <Fname>John</Fname> <Lname>Doe</Lname> <Record>F00025</Record> <Disease> <Disease_PK>1</Disease_PK> <Patient_FK>1</Patient_FK> <Date>3/3/2009</Date> <Dcode>D0456</Dcode> <Comm>comment</Comm> <Medicine> <Medicine_PK>1</Medicine_PK> <Disease_FK>1</Disease_FK> <Mcode>M00025</Mcode> <Qnt>0.01</Qnt> <Unit>l</Unit> </Medicine> </Disease> <Disease> <Disease_PK>2</Disease_PK> <Patient_FK>1</Patient_FK> <Date>3/15/2009</Date> <Dcode>D4415</Dcode> <Comm></Comm> </Disease> <Disease> <Disease_PK>3</Disease_PK> <Patient_FK>1</Patient_FK> <Date>3/19/2009</Date> <Dcode>D0176</Dcode> <Comm></Comm> <Medicine> <Medicine_PK>2</Medicine_PK> <Disease_FK>3</Disease_FK> <Mcode>M00015</Mcode> <Qnt>10</Qnt> <Unit>ml</Unit> </Medicine> <Medicine> <Medicine_PK>3</Medicine_PK> <Disease_FK>3</Disease_FK> <Mcode>M00006</Mcode> <Qnt>1</Qnt> <Unit>m</Unit> <Icode> <Medicine_FK>3</Medicine_FK> <Num>803125</Num> <Num>803005</Num> </Icode> </Medicine> </Disease> </Patient> <Patient> <Patient_PK>2</Patient_PK> <Fname>Jayne</Fname> <Lname>Joyce</Lname> <Record>F00156</Record> <Disease> <Disease_PK>4</Disease_PK> <Patient_FK>2</Patient_FK> <Date>3/18/2009</Date> <Dcode>D3266</Dcode> <Comm></Comm> <Medicine> <Medicine_PK>4</Medicine_PK> <Disease_FK>4</Disease_FK> <Mcode>M00006</Mcode> <Qnt>1</Qnt> <Unit>m</Unit> <Icode> <Medicine_FK>4</Medicine_FK> <Num>803125</Num> </Icode> </Medicine> </Disease> </Patient></root>\[/code\]Can anyone PLEASE help me?This is what I've done so far just by searching this site, but I'm new to XSLT so I'm stucked. I think I've got it right for the primary keys, but foreign are the problem, something's wrong. Is there a better way to just copy created primary key to a child elements?\[code\]<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method='xml' version='1.0' encoding='UTF-8' indent='yes' /> <xsl:template match="/ | @* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="Patient"> <xsl:variable name="PK"> <xsl:number level="any" count="Patient"/> </xsl:variable> <xsl:copy> <Patient_PK> <xsl:value-of select="$PK"/> </Patient_PK> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="Disease"> <xsl:variable name="PK"> <xsl:number level="any" count="Disease"/> </xsl:variable> <xsl:variable name="FK" select="count(../preceding-sibling::*) + 1"/> <xsl:copy> <Disease_PK> <xsl:value-of select="$PK"/> </Disease_PK> <Patient_FK> <xsl:value-of select="$FK"/> </Patient_FK> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="Medicine"> <xsl:variable name="PK"> <xsl:number level="any" count="Medicine"/> </xsl:variable> <xsl:variable name="FK" select="count(../preceding-sibling::*) + 1"/> <xsl:copy> <Medicine_PK> <xsl:value-of select="$PK"/> </Medicine_PK> <Disease_FK> <xsl:value-of select="$FK"/> </Disease_FK> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="Icode"> <xsl:variable name="PK"> <xsl:number level="any" count="Icode"/> </xsl:variable> <xsl:variable name="FK" select="count(../preceding-sibling::*) + 1"/> <xsl:copy> <Icode_PK> <xsl:value-of select="$PK"/> </Icode_PK> <Medicine_FK> <xsl:value-of select="$FK"/> </Medicine_FK> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template></xsl:stylesheet>\[/code\]
 
Back
Top