首页 > 解决方案 > 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/changeSetxsl:attributeCORE

我正在使用 xslt 2.0 和 saxon 9.8he。

标签: xsltsaxon

解决方案


使用单独的modeie 而不是<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"并为执行任何处理的模式设置更多模板。


推荐阅读