Использование XSLT
Тема на форуме [BL] Использование XSLT (https://rusmnb.ru/index.php?topic=24804.0)
Примеры использования XSLT для замены, удаления, добавления атрибутов и элементов в XML файлах (в теме разбиты на отдельные блоки для удобства копирования).
<!-- замена кода в исходном xml на код в xml текущего мода на примере <Settlement ... > ... </Settlement> в файле settlements.xml; ТРЕБУЕТСЯ СОЗДАНИЕ XML ФАЙЛА, в котором содержится новый код; используется, если нужно заменить элемент целиком -->
<xsl:template match="Settlement[@id='hideout_forest_1']"/>
<!-- СЛЕДУЮЩИЕ ЗАПИСИ НЕ ТРЕБУЮТ СОЗДАНИЯ XML ФАЙЛА, т.е. все изменения в исходный xml вносятся непосредственно через xslt -->
<!-- добавление отсутствующего атрибута "mother=" на примере <Hero ... /> в файле heroes.xml -->
<xsl:template match='Hero[@id="lord_goldenhorde_6_13"]'>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="mother">Hero.lord_goldenhorde_6_2</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<!-- удаление атрибута "mother" в исходном xml на примере <Hero ... /> в файле heroes.xml -->
<xsl:template match="Hero[@id='lord_goldenhorde_6_10']/@mother" />
<!-- замена атрибута "name" на примере <Settlement ... > ... </Settlement> в файле settlements.xml; я использую этот код, т.к. он самый короткий -->
<xsl:template match="Settlement[@id='Golden_castle_village_K1_1']/@name">
<xsl:attribute name="name">{=Settlements.Settlement.name.ECM.Golden_castle_village_K1_1}Beshtau</xsl:attribute>
</xsl:template>
<!-- замена атрибута "text" на примере <Settlement ... > ... </Settlement> в файле settlements.xml -->
<xsl:template match="Settlement[@id='Golden_castle_village_K1_1']/@text">
<xsl:attribute name="text">{=Settlements.Settlement.text.castle_village_K1_1}The village of Beshtau (Five Mountains) is located at the foot of the five-domed mountain of the same name, after which the village is named. Local Circassians are engaged not only in breeding horses. They spend several months of the year raiding their neighbors, looting.</xsl:attribute>
</xsl:template>
<!-- замена атрибута "village_type" на примере <Settlement ... > ... </Settlement> в файле settlements.xml -->
<xsl:template match="Settlement[@id='Golden_castle_village_K1_1']/Components/Village/@village_type">
<xsl:attribute name="village_type">VillageType.steppe_horse_ranch</xsl:attribute>
</xsl:template>
<!-- замена атрибута "name" на примере <NPCCharacter ... > ... </NPCCharacter>в файле lords.xml -->
<xsl:template match="NPCCharacter[@id='lord_goldenhorde_6_2']/@name">
<xsl:attribute name="name">{=ECM_lord_goldenhorde_6_2}Boraqchin</xsl:attribute>
</xsl:template>
<!-- замена атрибута "age" в дочернем узле на примере <NPCCharacter ... > ... </NPCCharacter>в файле lords.xml -->
<xsl:template match="NPCCharacter[@id='lord_goldenhorde_6_2']/face/BodyProperties/@age">
<xsl:attribute name="age">36</xsl:attribute>
</xsl:template>
<!-- замена нескольких одинаковых атрибутов "EquipmentSet" в дочернем узле на примере <NPCCharacter ... > ... </NPCCharacter>в файле lords.xml -->
<xsl:template match="NPCCharacter[@id='lord_goldenhorde_6_10']/Equipments">
<Equipments>
<EquipmentSet id="khu_bat_template_medium" />
<EquipmentSet id="khu_civ_template_default" civilian="true" />
</Equipments>
</xsl:template>
<!-- замена нескольких одинаковых атрибутов "name" в дочернем узле на примере <Culture ... > ... </Culture>в файле spcultures.xml -->
<xsl:template match="Culture[@id='empire']/clan_names">
<clan_names>
<name name="Acapanos" />
<name name="Basileus" />
</clan_names>
</xsl:template>
<!-- второй рабочий вариант кода для замены атрибутов; я его не использую, т.к. он громоздкий, возможно у него есть доп. возможности, о которых я не знаю, т.к. не программист -->
<xsl:template match="Settlement[@id='Golden_castle_K7']">
<Settlement>
<xsl:for-each select="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:for-each>
<xsl:attribute name="name">{=Settlements.Settlement.name.ECM.Golden_castle_K7}Hajji Tarkhan</xsl:attribute>
<xsl:apply-templates select="node()"/>
</Settlement>
</xsl:template>
<!-- третий вариант кода для замены атрибутов; ВНИМАНИЕ! В этом варианте текстовое значение внутри кавычек нужно дополнительно заключать в ' -->
<!-- замена атрибута "name" на примере <Settlement ... > ... </Settlement> в файле settlements.xml -->
<xsl:template match="Settlement[@id='Golden_castle_village_K1_1']/@name">
<xsl:attribute name="name">
<xsl:value-of select="'{=Settlements.Settlement.name.ECM.Golden_castle_village_K1_1}Beshtau'"/> <!-- "'текст'" -->
</xsl:attribute>
</xsl:template>
<!-- замена атрибута "difficulty" на примере <Item ... /> ... </Item> в файле weapons.xml -->
<xsl:template match="Item[@id='crossbow_f']/@difficulty">
<xsl:attribute name="difficulty">
<xsl:value-of select="75"/>
</xsl:attribute>
</xsl:template>
<!-- замена атрибута "missile_speed" в дочернем узле на примере <Item ... /> ... </Item> в файле weapons.xml -->
<xsl:template match="Item[@id='crossbow_f']/ItemComponent/Weapon/@missile_speed">
<xsl:attribute name="missile_speed">
<xsl:value-of select="102"/>
</xsl:attribute>
</xsl:template>