XSLT 1.0 如何在可选元素后更新或插入元素

问题描述 投票:0回答:1

这和这个问题类似。XSLT-1.0:如何在相对于父元素的特定位置插入一个元素。但那里的答案并不包括以下情况。tag3 在输入的XML中缺少

我想在一个XML文档中插入或更新一个标签(比如说tag4),这个标签看起来像下面,除了tag1、tag2、tag3、tag4和tag5都是可选的。

<Begin>
    <tag1 value="a" />
    <tag2 value="b" />
    <tag3 value="c" />
    <tag4 value="d" />
    <tag5 value="e" />
</Begin>

也就是说,以下是输入和输出的示例。

输入:

<Begin>
</Begin>

输出:

<Begin>
    <tag4 value="updated" />
</Begin>

输入:

<Begin>
    <tag4 value="d" />
</Begin>

輸出:

<Begin>
    <tag4 value="updated" />
</Begin>

输入: 输出: 输入:

<Begin>
    <tag1 value="a" />
    <tag5 value="e" />
</Begin>

输入:输出:输入:输出

<Begin>
    <tag1 value="a" />
    <tag4 value="updated" />
    <tag5 value="e" />
</Begin>

输入: 输入: 输出: 输出:

<Begin>
    <tag1 value="a" />
    <tag2 value="b" />
    <tag5 value="e" />
</Begin>

输入:输出:输入:输出

<Begin>
    <tag1 value="a" />
    <tag2 value="b" />
    <tag4 value="updated" />
    <tag5 value="e" />
</Begin>

输入: 输入: 输出: 输出:

<Begin>
    <tag1 value="a" />
    <tag2 value="b" />
    <tag3 value="c" />
    <tag5 value="e" />
</Begin>

输出: 输出: 输入: 输出: 输入: 输出:

<Begin>
    <tag1 value="a" />
    <tag2 value="b" />
    <tag3 value="c" />
    <tag4 value="updated" />
    <tag5 value="e" />
</Begin>

UPDATE

我还希望能够保留可能已经存在于 Begin 或 tag4 元素上的任何属性,例如。

输入:

<Begin someAttribute="someValue">
    <tag1 value="a" />
    <tag2 value="b" />
    <tag3 value="c" />
    <tag4 value="d" someOtherAttribute="someOtherValue" />
    <tag5 value="e" />
</Begin>

输出。

<Begin someAttribute="someValue">
    <tag1 value="a" />
    <tag2 value="b" />
    <tag3 value="c" />
    <tag4 value="updated" someOtherAttribute="someOtherValue" />
    <tag5 value="e" />
</Begin>
xslt xslt-1.0
1个回答
1
投票

试试吧。

<xsl:template match="Begin">
  <Begin>
    <xsl:copy-of select="tag1 | tag2 | tag3"/>
    <tag4 value="updated"/>
    <xsl:copy-of select="tag5"/>
  </Begin>
</xsl:template>

0
投票

试试这个... 如果需要的话,在调试器中跟踪它,以帮助你解决这个问题。

    <xsl:template match="Begin">
      <xsl:copy>
        <!-- Apply any attributes for Begin.  -->
        <xsl:apply-templates select="@*"/>

        <!-- Output the tag* nodes before tag4. -->
        <xsl:apply-templates select="tag1|tag2|tag3"/>

        <xsl:choose>
          <!-- Is there already a tag4? -->
          <xsl:when test="tag4">
            <xsl:apply-templates select="tag4"/>
          </xsl:when>
          <xsl:otherwise>
            <!-- Create tag4 -->
            <tag4 value="updated"/>
          </xsl:otherwise>
        </xsl:choose>

        <!-- Output the tag* nodes after tag4. -->
        <xsl:apply-templates select="tag5"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="tag4">
      <xsl:copy>
        <xsl:attribute name="value">d</xsl:attribute>
        <xsl:attribute name="someOtherAttribute">someOtherValue</xsl:attribute>
        <xsl:apply-templates select="node()"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>
© www.soinside.com 2019 - 2024. All rights reserved.