2017-10-11 97 views
0

我将外键IDREF从xml链接到XSLT时遇到问题。我尝试了很多方法来解决这个问题。但是,当我显示它时,我无法从XSLT中看到我的IDREF。例如,下面的代码应显示患者医生参考d1,d2等。但它不显示医生的任何内容。任何人都可以帮助我解决它?如何将IDREF从XML链接到XSLT

这里是我的XML代码:

<patient unique_no="p1" gender="F"> 
    <f_name>MMA</f_name> 

    <l_name>STONE</l_name> 

    <doctor-ref IDREF="d1"/> 
    <doctor-ref IDREF="d2"/> 
    </patient> 
    <doctor id="d1"> 
     <d_f_name>Chef Linguini</d_f_name> 
     <d_l_name>Gusteau</d_l_name> 
     <specification>&on;</specification> 
    </doctor> 
    <doctor id="d2"> 
     <d_f_name>Gordon</d_f_name> 
     <d_l_name>Ramsay</d_l_name> 
     <specification>&ENT;</specification> 
    </doctor> 
    <xsl:template match="/"> 
    <xsl:for-each select="hospital/patient"> 
     <xsl:sort select="f_name" />    
      <tr> 
     <td> 
      <xsl:value-of select="@unique_no" /> 
     </td> 
      <td> 
      <xsl:value-of select="f_name" /> 
      </td> 
      <td> 
      <xsl:value-of select="l_name" /> 
      </td> 
    <xsl:choose> 
    <xsl:when test="@gender='M'"> 
     <td bgcolor="red"> 
     <xsl:value-of select="@gender"/></td> 
    </xsl:when> 
    <xsl:when test="@gender='F'"> 
     <td bgcolor="Yellow"> 
     <xsl:value-of select="@gender"/></td> 
    </xsl:when> 
    <xsl:otherwise> 
     <td><xsl:value-of select="@gender"/></td> 
    </xsl:otherwise> 
    </xsl:choose> 
     <!-- <td>  
      <xsl:value-of select="@gender" /> 
      </td> --> 
      <td> 
    <!-- <xsl:value-of select="key('Medications', @Medications/@IDREF)"/> 
    --> 
     <xsl:value-of select="@Medications"/>   
      </td> 
      <td> 
      <xsl:value-of select="@doctor" /> 
      <xsl:if test="position() != last()"> 
     <xsl:text>, </xsl:text> 
    </xsl:if> 
      </td> 
     </tr> 
     </xsl:for-each> 
    </xsl:template> 

我的输出应该具有节目医生IDREF也。但它并没有show.Why? enter image description here

回答

0

<doctor>节点是<patient>一个兄弟节点即两者都是在<hospital>节点下的相同的水平。为了访问<patient>循环内的<doctor>节点,可以使用XPath axes

在这种情况下,由于所需的输出不共享,所以假设您需要输出中的@IDREFdoctor name。使用相同的XSL可以完成访问<doctor>节点。取决于输出,<td>内的XSL代码可以保留或丢弃。

<xsl:template match="/"> 
    <xsl:for-each select="hospital/patient"> 
     <xsl:sort select="f_name" /> 
     <tr> 
      ... 
      <!-- accessing the @IDREF attribute of <doctor-ref> --> 
      <td> 
       <xsl:for-each select="doctor-ref/@IDREF"> 
        <xsl:value-of select="." /> 
        <xsl:if test="position() != last()"> 
         <xsl:text>, </xsl:text> 
        </xsl:if> 
       </xsl:for-each> 
      </td> 
      <!-- accessing the matching @id of <doctor> with @IDREF of <doctor-ref> --> 
      <td> 
       <xsl:for-each select="doctor-ref/@IDREF"> 
        <xsl:variable name="idref" select="." /> 
        <xsl:value-of select="concat(ancestor::*/doctor[@id = $idref]/d_f_name, ' ', ancestor::*/doctor[@id = $idref]/d_l_name)" /> 
        <xsl:if test="position() != last()"> 
         <xsl:text>, </xsl:text> 
        </xsl:if> 
       </xsl:for-each> 
      </td> 
     </tr> 
    </xsl:for-each> 
</xsl:template> 

这使输出如下

<tr> 
    ... 
    <td>d1, d2</td> 
    <td>Chef Linguini Gusteau, Gordon Ramsay</td> 
</tr>