2009-10-28 71 views
1

存在XML元素,这可能是一个非常简单的答案,但我的生活,我不能弄明白。测试如果XSLT

我要显示,这取决于正在显示的子元素,但我不知道如何测试我想要的元素某些内容。我想看看如果启动,停止和记元素存在

<xsl:template match="protocol[@id=$protocolNumber]"> 
<h4><xsl:value-of select="$sectionNumber"/>.<xsl:value-of select="@id"/>&nbsp;<xsl:value-of select="@title"/></h4> 
<p> 
    <xsl:for-each select="child::*"> 
     <xsl:choose> 
      <xsl:when test="start"> 
       <span id="start"><xsl:value-of select="start[@level]" /></span> 
      </xsl:when> 
      <xsl:when test="stop"> 
       <span id="stop"><xsl:value-of select="stop[@level]" /></span> 
      </xsl:when> 
      <xsl:when test="note"> 
       <span id="note"><xsl:value-of select="note[@title]" />:&nbsp;<xsl:value-of select="note/text()" /></span> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="text()"/><br/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:for-each> 
</p> 

<protocol id="2" title="Post-Conversion Of VF/VT"> 
      <note title="criteria">Post-conversion treatment of VF or VT should only be started if the patient has regained a pulse of adequate rate, and has a supraventricular rhythm. If not, refer to other cardiac protocols as appropriate. All antiarrhythmics are contraindicated if ventricular escape rhythm is present.</note> 
      <start level="All Levels"/> 
      <step>Routine medical care.</step> 
      <stop level="EMT"/> 
      <stop level="EMT-I"/> 
      <start level="EMT-CC &amp; P"/> 
      <step> 
       If conversion results from defibrillation without any drug therapy:<br/><br/> 
       Amiodarone (Cordarone) 150 mg IV/IO Slow IV 
      </step> 
      <step>If Amiodarone was the drug resulting in conversion from VF/VT, no additional antiarrhythmic is required.</step> 
      <step> 
       If Lidocaine (Xylocaine) was the drug resulting in conversion from VF/VT:<br/><br/> 
       Repeat Lidocaine bolus 0.75 mg/kg IV/IO every 10 minutes up to a total cumulative dose of 3 mg/kg. 
      </step> 
      <step>If more than above listed doses are needed because of length of transport time, contact Medical Control.</step> 
     </protocol> 
+0

也许你在想的? – kurosch 2009-10-28 21:23:33

回答

5

xsl:for-each,上下文元素.是你遍历当前元素。当你写一个XPath表达式像start,这真的是一样的child::start。你想要的是self::start。另外请注意child::*是多余的 - 只需要*即可。

更地道的XSLT的办法就是重构这个到一个单独的一套模板,让模式匹配做的工作:

<xsl:template match="protocol[@id=$protocolNumber]"> 
    <h4><xsl:value-of select="$sectionNumber"/>.<xsl:value-of select="@id"/>&nbsp;<xsl:value-of select="@title"/></h4> 
    <p> 
    <!-- Applies templates to all child elements --> 
    <xsl:apply-templates/> 
    </p> 
</xsl:template> 

<xsl:template match="protocol/start"> 
    <span id="start"><xsl:value-of select="start/@level" /></span> 
</xsl:template> 

<xsl:template match="protocol/stop"> 
    <span id="stop"><xsl:value-of select="stop/@level" /></span> 
</xsl:template> 

<xsl:template match="protocol/note"> 
    <span id="note"><xsl:value-of select="note/@title" />:&nbsp;<xsl:value-of select="note" /></span> 
</xsl:template> 

<xsl:template match="protocol/*"> 
    <xsl:value-of select="."/> 
</xsl:template> 
+0

+1,但非$ protocolNumber协议元素的存在使我担心。 – 2009-10-28 21:28:29

+0

非常好,这使得更有意义。谢谢! – cfree 2009-10-28 21:35:58

1

我不知道你想做什么,但我看到了几个可能的问题:

首先,你用<xsl:choose />结构,这意味着如果你有“开始”没有“停止”和“说明”将被处理(你可能想使用纯<xsl:if />!而非,或任何所需的逻辑建议。

其次,当你使用[email protected],我相信你真的是start/@level

+0

其实我的意思是开始[@level],但无论哪种方式第一次都是错误的。谢谢。 我有一个循环,所以我假设每次迭代,它会遍历和选择,如果它开始,做一两件事。如果停止,做另一个等等,那是对的吗? – cfree 2009-10-28 21:23:23

+0

啊..我没有真正仔细阅读你的代码。它看起来像你在'test =“local-name()='start'”'和类似的。但帕维尔指出你更常见的做法。 – 2009-10-28 21:26:39