2012-06-14 137 views
1

我想使用xslt(主键PK和外键FK)在xml中创建父元素和子元素之间的关系。源XML是这样的:用于创建关系的XML XSLT转换主键和外键

<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> 

转换后的XML应该是这样的一个:

<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> 

这是我迄今所做的只是通过搜索这个网站,但我是新到XSLT,所以我被困住了。我认为我的主键是正确的,但外国人是问题,出了问题。有没有更好的方法来将创建的主键复制到子元素?

<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> 
+0

这很可能是有人可以帮助你...但不是完全为你做。告诉我们你试过的东西:) – Utkanos

+0

我已经添加了我迄今为止所尝试的... – user1455484

回答

1

这里是一个溶液

​​

当这个变换所提供的XML文档应用:

<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> 

有用,正确的结果产生:

<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/> 
     </Disease> 
     <Disease> 
     <Disease_PK>3</Disease_PK> 
     <Patient_FK>1</Patient_FK> 
      <Date>3/19/2009</Date> 
      <Dcode>D0176</Dcode> 
      <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> 
        <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/> 
      <Medicine> 
      <Medicine_PK>3</Medicine_PK> 
      <Disease_FK>4</Disease_FK> 
       <Mcode>M00006</Mcode> 
       <Qnt>1</Qnt> 
       <Unit>m</Unit> 
       <Icode> 
        <Num>803125</Num> 
       </Icode> 
      </Medicine> 
     </Disease> 
    </Patient> 
</root>