You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用XSLT为指定id的XML Culture节点添加template子元素

针对指定Culture节点添加template元素的XSLT方案

核心说明

不用怕<template>元素引发问题——XSLT里只有带xsl:前缀的<xsl:template>是关键字指令,你要输出的普通<template>只是普通XML元素,直接写就行。下面的XSLT会只修改id为"uc_dummy"的Culture节点,其他所有内容保持原样:

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <!-- 精准匹配目标节点:id为uc_dummy的Culture下的notable_and_wanderer_templates -->
    <xsl:template match="Culture[@id='uc_dummy']/notable_and_wanderer_templates">
        <!-- 先复制原节点的所有内容 -->
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <!-- 添加你需要的新template元素 -->
            <template name="NPCCharacter.uc_wanderer_empire_0" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

代码解释

  1. 身份模板是XSLT的基础,确保除了我们专门匹配的节点外,其他所有XML内容(包括其他Culture节点、属性、文本等)都原封不动复制。
  2. 目标匹配模板精准定位到你需要修改的节点,先复制它原有所有子元素,再追加新的<template>元素,不会影响其他任何节点。

示例效果

如果你的原XML片段是:

<Culture id="uc_dummy">
    <notable_and_wanderer_templates>
        <template name="existing_template_1" />
    </notable_and_wanderer_templates>
</Culture>
<Culture id="other_culture">
    <notable_and_wanderer_templates>
        <template name="existing_template_2" />
    </notable_and_wanderer_templates>
</Culture>

转换后会变成:

<Culture id="uc_dummy">
    <notable_and_wanderer_templates>
        <template name="existing_template_1" />
        <template name="NPCCharacter.uc_wanderer_empire_0" />
    </notable_and_wanderer_templates>
</Culture>
<Culture id="other_culture">
    <notable_and_wanderer_templates>
        <template name="existing_template_2" />
    </notable_and_wanderer_templates>
</Culture>

内容的提问来源于stack exchange,提问作者Salminar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 17:55:27