输入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>我需要在Id2之后添加两个元素Id3和Id1。
预期产出:-
<?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>我在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>如果可以通过xslt进行操作,请告诉我。感谢你的帮助!
发布于 2021-05-31 14:54:29
只需使用此模板:
<xsl:template match="Id1">
<xsl:next-match />
<Id2>C1</Id2>
<Id3>29</Id3>
</xsl:template>要使用XSLT1.0完成任务,您必须使用标识模板并将<xsl:next-match />替换为<xsl:copy-of select="." />,但是使用XSLT3.0,您可以使用<xsl:mode on-no-match="shallow-copy"/>,就像在示例中所做的那样。
结果是:
<?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>https://stackoverflow.com/questions/67775920
复制相似问题