2017-10-11 62 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>