xslt - XSLT 将属性添加到已处理节点并输出到结果文档
问题描述
这是示例 xml:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd
http://www.liquibase.org/xml/ns/dbchangelog">
<changeSet id="1" author="a">
<createTable tableName="TABLE1">
<column>
</column>
</createTable>
</changeSet>
<changeSet id="1-1" author="a">
<createSequence sequenceName="SEQ_TABLE1" />
</changeSet>
<changeSet id="4" author="A">
<createTable tableName="TABLE4">
<column>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
这是模板:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.liquibase.org/xml/ns/dbchangelog">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:variable name="coreTables" select="('TABLE1','TABLE2')"/>
<xsl:template match="node()[not(self::*)]">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="databaseChangeLog">
<!-- CORE-->
<xsl:comment>CORE TABLES</xsl:comment>
<xsl:variable name="coreTablesVariable" select="changeSet[createTable/@tableName=$coreTables]"/>
<xsl:comment>CORE SEQUENCES</xsl:comment>
<xsl:variable name="coreSequencesVariable" select="changeSet[createSequence[starts-with(@sequenceName, 'SEQ_') and substring-after(@sequenceName, 'SEQ_') = $coreTables]]"/>
<xsl:comment>CORE INDEXES</xsl:comment>
<xsl:variable name="coreIndexesVariable" select="changeSet[createIndex/@tableName=$coreTables]"/>
<xsl:comment>CORE FOREIGN CONSTRAINTS</xsl:comment>
<xsl:variable name="coreForeignConstraintsVariable" select="changeSet[addForeignKeyConstraint/@baseTableName=$coreTables]"/>
<xsl:comment>CORE VIEWS</xsl:comment>
<xsl:variable name="coreViewsVariable" select="changeSet[createView/@viewName=$coreTables]"/>
<xsl:call-template name="createChangeLog">
<xsl:with-param name="outputFile" select="'core-changelog.xml'"/>
<xsl:with-param name="changeLogContent" select="$coreTablesVariable,$coreSequencesVariable,$coreIndexesVariable,$coreForeignConstraintsVariable,$coreViewsVariable"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="createChangeLog">
<xsl:param name="outputFile"/>
<xsl:param name="changeLogContent"/>
<xsl:result-document encoding="UTF-8" indent="true" method="xml" href="{$outputFile}">
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd http://www.liquibase.org/xml/ns/dbchangelog" logicalFilePath="TODO">
<xsl:copy-of select="$changeLogContent"/>
</databaseChangeLog>
</xsl:result-document>
</xsl:template>
</xsl:transform>
我想将另一个属性( )添加到在createChangelogTemplate
每个元素内部处理的输出 xml 中。我试图添加另一个与其他元素匹配的模板,但这对我不起作用。如果有办法在一个地方做这件事会很好,因为我需要准备更多的部分,比如.<changeSet>
context="legacy"
databaseChangelog/changeSet
xsl:attribute
CORE
我正在使用 xslt 2.0 和 saxon 9.8he。
解决方案
使用单独的mode
ie 而不是<xsl:copy-of select="$changeLogContent"/>
use <xsl:apply-templates select="$changeLogContent" mode="legacy"/>
,然后设置 eg
<xsl:template match="changeSet" mode="legacy">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="context">legacy</xsl:attribute>
<xsl:copy-of select="node()"/>
</xsl:copy>
</xsl:template>
如果需要进一步处理属性和/或子节点,则更改<xsl:copy-of select="@*"/>
和/或<xsl:copy-of select="node()"/>
使用xsl:apply-templates mode="#current"
并为执行任何处理的模式设置更多模板。
推荐阅读
- c# - 使用 linq 在关系表中选择最大值
- javascript - 调试时搜索chrome源代码卡住
- parallel-processing - 如何在 Ray 中与多角色共享内存
- php - 在 laravel 中调用模型
- angularjs - 在使用 MSI 安装程序安装电子应用程序时创建 Windows 注册表项
- java - json-path 2.4.0 支持的 JDK 版本是多少?
- javascript - 无法使用 Selenium Web Driver C# 在文本区域中输入文本
- python - 我正在尝试检查括号是否完整并计算两个括号的完整和不完整,但我得到了不正确的输出
- node.js - 温斯顿设置时间戳:选项的 true 不起作用
- python-3.x - 无法在 DRF 的 get 函数中返回查询