How to link IDREF FROM XML TO XSLT

2019-08-17 08:21发布

I have a problem with linking foreign key IDREF from xml TO XSLT. I have tried in many ways to solve that problem. But still when I display it, I cannot see my IDREF from XSLT. For example the code below should display patient doctor-reference d1,d2 ...etc.But it does not display anything from the doctor .can anyone help me to solve it ??

Here is my code for 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> 

My output should have show doctor idref also .But it did not show.Why? enter image description here

标签: xml xslt
1条回答
一夜七次
2楼-- · 2019-08-17 09:06

The <doctor> node is a sibling of <patient> node i.e. both are at the same level under the <hospital> node. In order to access the <doctor> node from within the <patient> loop, XPath axes can be used.

In this scenario, since the required output is not shared, the assumption is that you need either the @IDREF or the doctor name in your output. Using the same XSL the below can be done to access the <doctor> nodes. Depending on the output the XSL code within the <td> can be retained or discarded.

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

This gives the output as below

<tr>
    ...
    <td>d1, d2</td>
    <td>Chef Linguini Gusteau, Gordon Ramsay</td>
</tr>
查看更多
登录 后发表回答