XSLT-1.0:如何在相对于父元素的特定位置插入元素 [英] XSLT-1.0: How to insert an element at a specific position with respect to the parent element

查看:28
本文介绍了XSLT-1.0:如何在相对于父元素的特定位置插入元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出子元素相对于其父元素的位置.
我有以下输入 XML:

I am trying to find out the position of a child element with respect to its parent.
I have the following input XML:

<Begin>
    <tag1>g</tag1>
    <tag2>b</tag2>
    <tag3>c</tag3>
    <tag5>e</tag5>
</Begin>

我需要知道相对于的位置,即3

I need to know the position of <tag3> with respect to <Begin>, i.e. 3

这可以在 XSLT 中完成吗?
我当前的 XSLT 看起来像这样:

Can this be done in XSLT?
My current XSLT looks like this:

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

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

  <xsl:template match="Begin[count(tag4)=0]">
    <xsl:copy>
      <!-- Need to find the position of /Begin/tag3" here -->
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

基本上,我想要做的是在输入 XML
中找到 的位置并使用位置之后插入.

Basically, what I am trying to do is to find the position of <tag3> in the input XML
and insert <tag4> exactly after <tag3> using the position.

所以我的问题是:
如何在 之间的位置插入元素 ?
这就是我的意图!

So my question is:
How can I insert an element <tag4> at the position between <tag3> and <tag5>?
This is my intent!

推荐答案

插入按字母顺序排序的新元素的一种简单方法是在 identity 模板中使用 像这样:

An easy way to insert a new element sorted alphabetically is to use <xsl:sort> in the identity template like this:

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

此修改确实复制了按本地名称/标签名称/元素名称字母顺序排序的所有元素.

This modification does copy all elements sorted alphabetically by the local-name/tag-name/element-name.

这篇关于XSLT-1.0:如何在相对于父元素的特定位置插入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆