Xml 如何使用xslt插入/替换元素

Xml 如何使用xslt插入/替换元素,xml,xslt,Xml,Xslt,我有以下xml文件 <?xml version="1.0" encoding="UTF-8"?> <application xmlns:t="http://www.tibco.com/xmlns/ApplicationManagement" name="test"> <content> <on> false </on> </content> </application>

我有以下
xml
文件

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:t="http://www.tibco.com/xmlns/ApplicationManagement"    
name="test">
    <content>
        <on> false </on>
    </content>
</application>
我对
新on值
有问题,因为解决方案应该替换
on
标记值,而是创建一个全新的值

结果如下(不包括最上面的xml和应用程序标记):


假的
轻的
新身份
新价值观

如何在同一模板中替换on标记?

您可以使用模板匹配来匹配
元素和
元素(不是示例的一部分)


添加了新状态
增值
新价值观
新身份
输出:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:t="http://www.tibco.com/xmlns/ApplicationManagement" name="test">
    <content>
        <on xmlns="http://www.w3.org/1999/xhtml">new on value</on>        
        <status xmlns="http://www.w3.org/1999/xhtml">added new status </status>
    </content>
</application>

新价值观
添加了新状态

您可以使用模板匹配来匹配
元素和
元素(不是示例的一部分)


添加了新状态
增值
新价值观
新身份
输出:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:t="http://www.tibco.com/xmlns/ApplicationManagement" name="test">
    <content>
        <on xmlns="http://www.w3.org/1999/xhtml">new on value</on>        
        <status xmlns="http://www.w3.org/1999/xhtml">added new status </status>
    </content>
</application>

新价值观
添加了新状态

在副本中,您告诉它处理内容的所有子节点。如果您不希望它处理现有的on标记,则需要将其从副本中排除。如果您不想复制其他子节点,可以跳过该行。省略该行的问题是,如果在我的原始xml中,我有一个标记,上面写着
one
,而我省略了应用模板,那么该模板将不会被复制到您的副本中,您告诉它
处理内容的所有子节点。如果您不希望它处理现有的on标记,则需要将其从副本中排除。如果您不想复制其他子节点,可以跳过该行。省略该行的问题是,如果在我的原始xml中,我有一个标记,上面写着
one
,而我省略了apply模板,那么它将不会被复制如果您想
insert
on,如果它不存在?如果你想在它不存在的情况下插入,你会怎么做?
<xsl:template match="content">                  <!-- remove <xsl:if...> from this template -->
    <xsl:copy>
        <xsl:apply-templates />
        <xsl:if test="not(status)">             <!-- if <status> element does not exist, create one -->
            <status>added new status </status>
        </xsl:if>
        <xsl:if test="not(on)">                 <!-- if <on> element does not exist, create one -->
            <on>added new on value</on>
        </xsl:if>
    </xsl:copy>
</xsl:template>

<xsl:template match="on[parent::content]">      <!-- replaces <on> elements with parent <content> -->
    <on>new on value</on>
</xsl:template>

<xsl:template match="status[parent::content]">  <!-- replaces <status> elements with parent <content> -->  
    <status>new status </status>
</xsl:template>
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:t="http://www.tibco.com/xmlns/ApplicationManagement" name="test">
    <content>
        <on xmlns="http://www.w3.org/1999/xhtml">new on value</on>        
        <status xmlns="http://www.w3.org/1999/xhtml">added new status </status>
    </content>
</application>