xml - XSL 检查直接在前的兄弟

标签 xml xslt

在过去的两天里,我的大脑放了一个大屁。我需要检查第一个 <step1> <proc> 的子元素有 <note> 的前一个 sibling :

<proc>
   <note>
       <trim.para>Some sort of note</trim.para>
   </note>
   <step1>
       <para>Turn off all electrical power .</para>
   </step1>
   <step1>
       <para>A second step.</para>
   </step1>
</proc>

我正在尝试转换 <step1>元素(和子元素 <step2> 元素)到 <proceduralStep>具有以下 XSL 的元素,但没有任何运气获得 <note>包括在内:

<xsl:template match="step1 | step2">
        <!-- create key/value pair to store existing ids with new value for proceduralStep element-->
        <xsl:choose>
            <xsl:when test="./@id">
                <proceduralStep id="{@id}">
                    <xsl:if test="preceding-sibling::*[1][note]">
                        <xsl:for-each select="preceding-sibling::*[1][note]">
                            <note>
                                <notePara>
                                    <xsl:value-of select="./trim.para"/>
                                </notePara>
                            </note>
                        </xsl:for-each>
                    </xsl:if>
                        <xsl:apply-templates/>
                </proceduralStep>
            </xsl:when>
            <xsl:otherwise>
                <proceduralStep>
                    <xsl:if test="preceding-sibling::*[1][note]">
                        <xsl:for-each select="preceding-sibling::*[1][note]">
                            <note>
                                <notePara>
                                    <xsl:value-of select="./trim.para"/>
                                </notePara>
                            </note>
                        </xsl:for-each>
                    </xsl:if>
                        <xsl:apply-templates/>
                </proceduralStep>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

这个输出:

<mainProcedure>
    <proceduralStep>
      <para>Turn off all electrical power.</para>
    </proceduralStep>
    <proceduralStep>
      <para>A second step.</para>
    </proceduralStep>
</mainProcedure>

我需要这个来输出:

<mainProcedure>
    <proceduralStep>
        <note>
            <notePara>Some sort of note</notePara>
        </note>
       <para>Turn off all electrical power.</para>
    </proceduralStep>
    <proceduralStep>
      <para>A second step.</para>
    </proceduralStep>
</mainProcedure>

我已经有一个模板可以处理 <note>当元素出现在 <step1> 内部时或 <step2>元素(或其他任何地方)。我创建了一个空模板来禁止将 proc\note 放在我的输出中:

<xsl:template match="note[parent::proc]"></xsl:template>

<xsl:template match="note">
    <note>
      <xsl:apply-templates/>
    </note>
</xsl:template>

<xsl:template match="note/trim.para">
    <notePara>
        <xsl:apply-templates/>
     </notePara>
</xsl:template>

我发誓我以前做过这件事,但我只是今天早上(或昨天早上)不记得了。

最佳答案

正如 Martin 在他的评论中建议的那样,您需要使用 self:: 轴来检查前面的同级。您还可以去掉 if,并通过复制现有的 id 属性显着减少此处的代码重复:

<xsl:template match="step1 | step2">
    <!-- create key/value pair to store existing ids with new value for proceduralStep
         element-->
    <proceduralStep>
        <!-- copy id attribute from step1|step2 if it exists, do nothing if not -->
        <xsl:copy-of select="@id" />
        <!-- no need for an if here - if the immediately preceding sibling is not a
             note then the select will return an empty node set, which makes the
             for-each a no-op -->
        <xsl:for-each select="preceding-sibling::*[1][self::note]">
            <note>
                <notePara>
                     <xsl:value-of select="./trim.para"/>
                </notePara>
            </note>
        </xsl:for-each>
        <xsl:apply-templates/>
    </proceduralStep>
</xsl:template>

关于xml - XSL 检查直接在前的兄弟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28984806/

相关文章:

Python 将 unicode 保存到 XML

xml - 如何为 XML 节点的无序列表创建具有出现约束的模式

xslt - 无法获得正确的 XSLT 输出

xslt 命名模板 : how to write a template that creates a string representing the xpath of a given node

xslt 在元素不存在的地方添加默认值

javascript - 将 XML 解析为 Javascript 对象

SqlBulkCopy 无法尝试复制 XML 列中包含大量内容的行

xml - 如何用替换br标签 XSL模板中的标签?

html - 为什么我的 XSLT 在这里剥离 HTML 标签

c - 保存简单的 XML 文件