XSLT,用它的祖先获取节点

时间:2015-07-13 23:29:40

标签: xslt-2.0

我需要弄清楚如何用它的祖先提取节点。例如,给定值为" Spine Percutaneous Interventions"

的映射
  <mdCategoryMapping>
      <mdCategory title="Cerebrovascular" order="20">
         <mdCategory title="Endovascular Surgical Neuroradiology" order="230">
            <mdCategory title="Aneurysms and Subarachnoid Hemorrhage" order="2310" />
            <mdCategory title="Brain Arteriovenous Malformations" order="2320" />
            <mdCategory title="Cranial Dural Arteriovenous Shunts" order="2330" />
            <mdCategory title="Head and Neck Vascular Lesions" order="2340" />
            <mdCategory title="Pediatric Vascular Interventions" order="2350" />
            <mdCategory title="Spine Percutaneous Interventions" order="2360" />
            <mdCategory title="Spine Vascular Interventions" order="2365" />
            <mdCategory title="Stroke" order="2370" />
            <mdCategory title="Trauma" order="2380" />
            <mdCategory title="Tumors" order="2390" />
         </mdCategory>
      </mdCategory>
  </mdCategoryMapping>

我需要以下结果:

<mdCategory title="Cerebrovascular" order="20">
    <mdCategory title="Endovascular Surgical Neuroradiology" order="230">
        <mdCategory title="Spine Percutaneous Interventions" order="2360" />
    </mdCategory>
</mdCategory>

当然,当$ next-cat等于&#34; Spine Percutaneous Interventions&#34;

时,以下仅为我提供了最低级别的类别。

    <xsl:copy-of select="//enes:metaInfo/enes:mdCategoryMapping//enes:mdCategory[@title = $next-cat]" />

结果:

<mdCategory title="Spine Percutaneous Interventions" order="2360" />

同样,当$ next-cat等于&#34; Cerebrovascular&#34;我得到了所有子节点的整棵树。

如何获得具有祖先的最低级别节点或仅包含选定子节点的顶级节点?

1 个答案:

答案 0 :(得分:1)

如果您知道如何选择您感兴趣的元素或您感兴趣的元素,那么您可以选择它们,选择它们的祖先并确保您的模板只复制这些节点:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="next-cat" select="'Spine Percutaneous Interventions'"/>

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:variable name="selected-cat" select="//mdCategory[@title = $next-cat]"/>
<xsl:variable name="subtree" select="$selected-cat/ancestor-or-self::*"/>

<xsl:template match="/">
  <xsl:apply-templates select="$subtree[2]"/>
</xsl:template>

<xsl:template match="@*">
  <xsl:copy/>
</xsl:template>

<xsl:template match="*[. intersect $subtree]">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()[. intersect $subtree]"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
相关问题