[英]How to link IDREF FROM XML TO XSLT
我在将外键IDREF从xml链接到XSLT时遇到问题。 我已经尝试了许多方法来解决该问题。 但是,当我显示它时,仍然看不到来自XSLT的IDREF。 例如下面的代码应该显示病人的医生参考d1,d2 ...等,但是它不显示医生的任何信息。有人可以帮助我解决这个问题吗?
这是我的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>
我的输出还应该显示show医生idref,但是没有显示,为什么? 在此处输入图片说明
<doctor>
节点是<patient>
节点的同级,即,两者在<hospital>
节点下处于同一级别。 为了从<patient>
循环中访问<doctor>
节点,可以使用XPath axes
。
在这种情况下,由于未共享所需的输出,因此假设您在输出中需要@IDREF
或doctor 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.