使用xslt在父节点中创建标签

假设我有一个类似这样的XML文档(请注意,出于我的目的,标签在最终文档中的显示顺序无关紧要,以防有所不同;我们只是将XML用作花键/值对配置)

# -*- coding: utf-8 -*-
"""
Created on Mon Nov  4 20:16:13 2019

@author: Roberto Bellarosa
"""

import builtins

def fake_input(prompt):
    print(prompt)
    return "trgy"

builtins.input = fake_input

name = input("Enter your name >")

print("Hello,"+name+"!")

请注意,GoldenTag的值并不总是1234;这只是为了说明。现在,我想创建一个SilverTag,它是MyRoot的(直接)子代,假设GoldenTag存在并且满足其他各种条件,其值与GoldenTag相同。

我看不到这样做的好方法,因为如果我使用<MyRoot> various tags here I don't care about at the moment <child><GoldenTag>1234</GoldenTag></child> </MyRoot> ,则模板的内容最终会出现在template mach='GoldenTag'标记内,而不是我想要的位置。

tjlyu 回答:使用xslt在父节点中创建标签

MyRoot上的任何匹配当然也可以包括其他元素的条件,例如

<xsl:template match="MyRoot[child/GoldenTag and your-other-conditions]">
  <xsl:copy>
      <xsl:apply-templates/>
      <SilverTag><xsl:value-of select="child/GoldenTag"/></SilverTag>
  </xsl:copy>
</xsl:template>
,

根据马丁的回答,我认为(希望吗?)这样的方法应该起作用:

<xsl:template match="child[GoldenTag and other-tests]">
    <xsl:copy-of select="."/>
    <SilverTag>
        <xsl:value-of select="GoldenTag"/>
    </SilverTag>
</xsl:template>
本文链接:https://www.f2er.com/3164187.html

大家都在问