简体   繁体   English

使用xslt在特定点添加xml节点

[英]Adding xml nodes at a specific point using xslt

I have the following xml and want to insert additional xml into it: 我有以下xml,并希望在其中插入额外的xml:

<root>
    <steps>
        <step name="step1" type="process">
            <steps>
                <step name="substep1">
                </step>
            </steps>
        </step>
        <step name="step2" type="process">
            <steps>
                <step name="substep1">
                <!-- more substeps...-->
                </step>
            </steps>
        </step>
        <step name="step3" type="process">
            <steps>
                <step name="substep1">
                </step>
                <step name="substep2">
                </step>
                <!-- more substeps...-->
            </steps>
        </step>
        <!-- THE BELOW IS WHAT I WISH TO ADD... and it has to be here -->
        <step name="reference">
            <!-- These stuff have been hardcoded in my xsl so its fine -->
        </step>
        <!-- ends -->
    </steps>
    <references>
        <reference name="reference1">
        </reference>
        .
        .
        .
    </references>
</root>

As I've written in the xml sample, I wish to add an additional step element as the very last step within the outter most steps. 正如我在xml示例中所写的那样,我希望在大多数步骤中添加一个额外的步骤元素作为最后一步。 I have the xml snippet already hardcoded in my xsl so all I needed to do is to find the best logic to move to that particular point of the xml tree so I can call the template and have that snipet added in. 我已经在我的xsl中硬编码了xml片段,所以我需要做的就是找到移动到xml树特定点的最佳逻辑,这样我就可以调用模板并添加snipet了。

What is the recommended/best approach to do this? 这样做的推荐/最佳方法是什么?

Thanks. 谢谢。

This transformation : 这种转变

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 exclude-result-prefixes="xsl">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pAddition">
   <step name="reference">
     <XXX/>
   </step>
 </xsl:param>

 <xsl:template match="node()|@*" name="identity">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/*/steps/step[position()=last()]">
  <xsl:call-template name="identity"/>
  <xsl:copy-of select="$pAddition"/>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document : 应用于此XML文档时

<root>
    <steps>
        <step name="step1" type="process">
            <steps>
                <step name="substep1">
                </step>
            </steps>
        </step>
        <step name="step2" type="process">
            <steps>
                <step name="substep1">
                <!-- more substeps...-->
                </step>
            </steps>
        </step>
        <step name="step3" type="process">
            <steps>
                <step name="substep1">
                </step>
                <step name="substep2">
                </step>
                <!-- more substeps...-->
            </steps>
        </step>
    </steps>
    <references>
        <reference name="reference1">
        </reference>
        .
        .
        .
    </references>
</root>

produces the wanted, correct result : 产生想要的,正确的结果

<root>
   <steps>
      <step name="step1" type="process">
         <steps>
            <step name="substep1"/>
         </steps>
      </step>
      <step name="step2" type="process">
         <steps>
            <step name="substep1"><!-- more substeps...--></step>
         </steps>
      </step>
      <step name="step3" type="process">
         <steps>
            <step name="substep1"/>
            <step name="substep2"/><!-- more substeps...-->
         </steps>
      </step>
      <step name="reference">
         <XXX/>
      </step>
   </steps>
   <references>
      <reference name="reference1"/>
        .
        .
        .
    </references>
</root>

Do note : 请注意

  1. **The identity rule is used to copy all nodes as is. ** 标识规则用于按原样复制所有节点。

  2. The XML fragment to be inserted is specified (for convenience) as the body of a global xsl:param . 要插入的XML片段(为方便起见)指定为全局xsl:param的主体。 In a realistic application it will be in a separate xml file and will be obtained using the xslt document() function. 在实际应用程序中,它将位于单独的xml文件中,并将使用xslt document()函数获取。

  3. The template that matches the insertion point copies the matched node by calling the identity rule. 与插入点匹配的模板通过调用标识规则来复制匹配的节点。 then it copies the content of the xsl:param and thus the new conted is output exactly at the desired insertion point. 然后它复制xsl:param的内容,因此新的conted将精确地输出到所需的插入点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM