测试XSLT中是否存在XML元素

cfr*_*ree 1 xml xslt

这可能是一个非常简单的答案,但对于我的生活,我无法弄明白.

我想根据显示的子元素显示某些内容,但我不知道如何测试我想要的元素.我想看看是否存在start,stop和note元素

<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>
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

Pav*_*aev 5

在内部xsl:for-each,context元素.是您正在迭代的当前元素.当你编写一个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>
Run Code Online (Sandbox Code Playgroud)