Xml XSLT:为其添加新标记和值

Xml XSLT:为其添加新标记和值,xml,xslt,Xml,Xslt,我试图在XSLT中添加具有值的新字段。我发现下面的链接很有用,但我只能添加一个字段和值。我想为它添加多个字段和值 供参考: 输入: <root> <item> <country>Brobdingnag</country> </item> <item> <test/> </item> </root> 布罗丁纳格

我试图在XSLT中添加具有值的新字段。我发现下面的链接很有用,但我只能添加一个字段和值。我想为它添加多个字段和值

供参考:

输入:

 <root>
     <item>
        <country>Brobdingnag</country>
      </item>
    <item>
        <test/>
     </item>
 </root>

布罗丁纳格
XSLT:


小人
输出:

  <root>
     <item>
         <country>Brobdingnag</country>
     </item>
     <item>
        <test></test>
        <country>Lilliput</country>
    </item>
 </root>

布罗丁纳格
小人

就像我需要添加多个字段一样。。任何帮助都将不胜感激。谢谢。

如果我正确理解了问题,您只需要一个附加条件

输入

<root>
  <item>
    <country>Brobdingnag</country>
    <state>State of Fiction</state>
  </item>
  <item>
    <test/>
  </item>
</root>

布罗丁纳格
虚构状态
xslt


小人
输出

 <root>
   <item>
     <country>Brobdingnag</country>
     <state>State of Fiction</state>
   </item>
   <item>
     <test></test>
     <country>Lilliput</country>
   </item>
 </root>

布罗丁纳格
虚构状态
小人
它是这样工作的

输入:

<root>
    <item>
        <country>Brobdingnag</country>
    </item>
    <item>
        <test/>
    </item>
</root>

布罗丁纳格
XSLT:


小人
虚构状态
输出:

 <root>
    <item>
        <country>Brobdingnag</country>
    </item>
    <item>
       <test></test>
       <country>Lilliput</country>
       <state>State of Fiction</state>
   </item>
 </root>

布罗丁纳格
小人
虚构状态

请提供有关特定xml/xsl输出/预期输出的更多详细信息。已编辑并再次提供链接以供参考。谢谢,基于这些输入,您的预期输出是什么?根据描述,我不确定我是否理解你想做什么。对不起,我不清楚。在上面的示例中,添加了一个新字段。“国家”。如果我试图再添加一个字段,请说“State”。。Lilliput被xxx替换,我希望两个字段都被添加。对不起,目前我还没有信心尝试回答。也许你的意思是想要像“”这样的东西来避免新节点?我在另一个答案中已经清楚地提到了。谢谢你的帮助。非常感谢。
<root>
    <item>
        <country>Brobdingnag</country>
    </item>
    <item>
        <test/>
    </item>
</root>
  <xsl:stylesheet version="1.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="item[not(country) and not(State)]">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
        <country>Lilliput</country>
        <state>State of Fiction</state>
    </xsl:copy>
   </xsl:template>
 </xsl:stylesheet>
 <root>
    <item>
        <country>Brobdingnag</country>
    </item>
    <item>
       <test></test>
       <country>Lilliput</country>
       <state>State of Fiction</state>
   </item>
 </root>