通过XSLT在其他两个元素之间添加元素?

时间:2022-12-02 14:09:25

I have the following input XML:

我有以下输入XML:

<root>
    <aaa>some string aaa</aaa>
    <bbb>some string bbb</bbb>
    <ddd>some string ddd</ddd> 
</root>

Using XSLT I want to the following output:

使用XSLT,我希望输出如下:

<root>
    <aaa>some string aaa</aaa>
    <bbb>some string bbb</bbb>
    <ccc>some string ccc</ccc>
    <ddd>some string ddd</ddd>
</root>

My XSLT is:

我的XSLT是:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="root">
        <root>
            <ccc>some string ccc</ccc>
            <xsl:apply-templates select="@*|node()"/> 
        </root>
    </xsl:template>
</xsl:stylesheet>

But I'm not getting my desired output. How could I put the ccc element between bbb and ddd elements using the identity template?

但是我没有得到我想要的输出。如何使用标识模板将ccc元素放在bbb和ddd元素之间?

I can use XSLT 3.0 if that helps.

如果有帮助的话,我可以使用XSLT 3.0。

2 个解决方案

#1


2  

Kenneth's answer is fine but as the question is tagged as XSLT 3.0 it can be written more compact so I add this answer as an alternative

Kenneth的答案很好,但是由于问题被标记为XSLT 3.0,所以可以写得更紧凑一些,因此我添加这个答案作为替代

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

    <xsl:output indent="yes"/>

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="ddd">
        <ccc>some string ccc</ccc>
        <xsl:next-match/>
    </xsl:template>

</xsl:stylesheet>

using <xsl:mode on-no-match="shallow-copy"/> to express the identity transformation and using <xsl:next-match/> to delegate copying of the ddd element to it.

使用 来表示标识转换,使用 将ddd元素的复制委托给它。

#2


2  

Use the identity transformation with a second template that matches the element before or after the insertion point, and then insert the new element after or before copying over the matched element. To wit:

使用与插入点之前或之后匹配元素的第二个模板的标识转换,然后在复制匹配的元素之前或之后插入新元素。即:

Given this input XML,

鉴于这种输入XML,

<root>
   <aaa>some string aaa</aaa>
   <bbb>some string bbb</bbb>
   <ddd>some string ddd</ddd> 
</root>

this XSLT,

这个XSLT,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

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

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

</xsl:stylesheet>

will generate this output XML:

将生成这个输出XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <aaa>some string aaa</aaa>
   <bbb>some string bbb</bbb>
   <ccc>some string ccc</ccc>
   <ddd>some string ddd</ddd> 
</root>

#1


2  

Kenneth's answer is fine but as the question is tagged as XSLT 3.0 it can be written more compact so I add this answer as an alternative

Kenneth的答案很好,但是由于问题被标记为XSLT 3.0,所以可以写得更紧凑一些,因此我添加这个答案作为替代

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

    <xsl:output indent="yes"/>

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="ddd">
        <ccc>some string ccc</ccc>
        <xsl:next-match/>
    </xsl:template>

</xsl:stylesheet>

using <xsl:mode on-no-match="shallow-copy"/> to express the identity transformation and using <xsl:next-match/> to delegate copying of the ddd element to it.

使用 来表示标识转换,使用 将ddd元素的复制委托给它。

#2


2  

Use the identity transformation with a second template that matches the element before or after the insertion point, and then insert the new element after or before copying over the matched element. To wit:

使用与插入点之前或之后匹配元素的第二个模板的标识转换,然后在复制匹配的元素之前或之后插入新元素。即:

Given this input XML,

鉴于这种输入XML,

<root>
   <aaa>some string aaa</aaa>
   <bbb>some string bbb</bbb>
   <ddd>some string ddd</ddd> 
</root>

this XSLT,

这个XSLT,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

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

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

</xsl:stylesheet>

will generate this output XML:

将生成这个输出XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <aaa>some string aaa</aaa>
   <bbb>some string bbb</bbb>
   <ccc>some string ccc</ccc>
   <ddd>some string ddd</ddd> 
</root>