2009-10-28 74 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