[英]Adding element in the middle of xml using xslt
Input XML Below:-输入 XML 下面:-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Change xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Area>
<Sender>
<LogicalId>tyhu</LogicalId>
</Sender>
<CreationDateTime>2021-04-29T14:33:13Z</CreationDateTime>
<Id1>
<Id>163067354</Id>
</Id1>
</Area>
<Data>
<Prob>
<DateTime>2021-04-29T14:33:13Z</DateTime>
</Prob>
</Data>
</Change>
I need to add two elements Id2 and Id3 after Id1.我需要在 Id1 之后添加两个元素 Id2 和 Id3。
Desired Output:-所需的 Output:-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Change xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Area>
<Sender>
<LogicalId>tyhu</LogicalId>
</Sender>
<CreationDateTime>2022-04-29T14:33:13Z</CreationDateTime>
<Id1>
<Id>6654</Id>
</Id1>
<Id2>C1</Id2>
<Id3>29</Id3>
</Area>
<Data>
<Prob>
<DateTime>2022-04-29T14:33:13Z</DateTime>
</Prob>
</Data>
</Change>
I tried below xslt but no luck:-我在 xslt 下面试过,但没有运气:-
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="Id1">
<Id2>C1</Id2>
<Id3>29</Id3>
<xsl:next-match/>
</xsl:template>
</xsl:stylesheet>
Please let me know if it is possible to do through xslt.请让我知道是否可以通过 xslt 完成。 Appreciate your help!感谢你的帮助!
Simply use this template:只需使用此模板:
<xsl:template match="Id1">
<xsl:next-match />
<Id2>C1</Id2>
<Id3>29</Id3>
</xsl:template>
To complete the task with XSLT-1.0, you would have to use the identity template and replace the <xsl:next-match />
by <xsl:copy-of select="." />
要使用 XSLT-1.0 完成任务,您必须使用标识模板并将<xsl:next-match />
替换为 < <xsl:copy-of select="." />
<xsl:copy-of select="." />
, but with XSLT-3.0, you can use the <xsl:mode on-no-match="shallow-copy"/>
, as you did in your example. <xsl:copy-of select="." />
,但是对于 XSLT-3.0,您可以使用<xsl:mode on-no-match="shallow-copy"/>
,就像您在示例中所做的那样。
The result is:结果是:
<?xml version="1.0" encoding="UTF-8"?>
<Change xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Area>
<Sender>
<LogicalId>tyhu</LogicalId>
</Sender>
<CreationDateTime>2021-04-29T14:33:13Z</CreationDateTime>
<Id1>
<Id>163067354</Id>
</Id1>
<Id2>C1</Id2>
<Id3>29</Id3>
</Area>
<Data>
<Prob>
<DateTime>2021-04-29T14:33:13Z</DateTime>
</Prob>
</Data>
</Change>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.