来自错误元素的属性的 xslt 副本

xslt copy of attribute from wrong element

我正在使用这个模板:

<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               xpath-default-namespace="http://www.liquibase.org/xml/ns/dbchangelog"
               xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
               xmlns:uuid="http://uuid.util.java"
               exclude-result-prefixes="uuid">

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="changeSet[createTable]">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="createTable"/>
            <xsl:copy-of select="*[not(self::createTable)]"/>
        </xsl:copy>
        <xsl:if test="createTable/@remarks">
            <xsl:element name="changeSet">
                <xsl:attribute name="id" select="uuid:new-uuid()"/>
                <xsl:attribute name="author">system</xsl:attribute>
                <xsl:element name="setTableRemarks">
                    <xsl:copy-of select="createTable//(@tableName,@remarks)" />
                </xsl:element>
                <xsl:for-each select="createTable/column[@remarks]">
                    <xsl:element name="setColumnRemarks">
                        <xsl:copy-of select="../@tableName" />
                        <xsl:attribute name="columnName" select="@name"/>
                        <xsl:copy-of select="@remarks"/>

                    </xsl:element>
                </xsl:for-each>
            </xsl:element>
        </xsl:if>
    </xsl:template>

    <xsl:template match="createTable/@remarks"/>
    <xsl:template match="createTable/column/@remarks"/>

</xsl:transform>

像这样变换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">
    <changeSet author="system (generated)" id="1538720867962-1">
        <createTable remarks="Journal event detail attribute mapping" tableName="AS_JOURNALEVENTDETAILATTRMAP">
            <column name="JOURNALEVENTTYPEID" remarks="Journal event type identifier" type="NUMBER(9, 0)">
                <constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
            </column>
            <column name="JOURNALEVENTDETAILATTRID" remarks="Journal event detail attribute identifier" type="NUMBER(9, 0)">
                <constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
            </column>
            <column name="LISTORDER" remarks="Order in list" type="NUMBER(9, 0)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
</databaseChangeLog>

我希望结果是:

<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">
   <changeSet author="system (generated)" id="1538720867962-1">
      <createTable tableName="AS_JOURNALEVENTDETAILATTRMAP">
         <column name="JOURNALEVENTTYPEID" type="NUMBER(9, 0)">
            <constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
         </column>
         <column name="JOURNALEVENTDETAILATTRID" type="NUMBER(9, 0)">
            <constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
         </column>
         <column name="LISTORDER" type="NUMBER(9, 0)">
            <constraints nullable="false"/>
         </column>
      </createTable>
   </changeSet>
   <changeSet id="c85f187d-f917-4948-8c48-7b1a132dd79e" author="system">
      <setTableRemarks remarks="Journal event detail attribute mapping" tableName="AS_JOURNALEVENTDETAILATTRMAP"/>
      <setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
                        columnName="JOURNALEVENTTYPEID"
                        remarks="Journal event type identifier"/>
      <setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
                        columnName="JOURNALEVENTDETAILATTRID"
                        remarks="Journal event detail attribute identifier"/>
      <setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
                        columnName="LISTORDER"
                        remarks="Order in list"/>
   </changeSet>
</databaseChangeLog>

但我得到的不是这个:

<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">
   <changeSet author="system (generated)" id="1538720867962-1">
      <createTable tableName="AS_JOURNALEVENTDETAILATTRMAP">
         <column name="JOURNALEVENTTYPEID" type="NUMBER(9, 0)">
            <constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
         </column>
         <column name="JOURNALEVENTDETAILATTRID" type="NUMBER(9, 0)">
            <constraints primaryKey="true" primaryKeyName="PK$AS_JOURNALEVENTDETATTRMAP"/>
         </column>
         <column name="LISTORDER" type="NUMBER(9, 0)">
            <constraints nullable="false"/>
         </column>
      </createTable>
   </changeSet>
   <changeSet id="c85f187d-f917-4948-8c48-7b1a132dd79e" author="system">
      <setTableRemarks remarks="Order in list" tableName="AS_JOURNALEVENTDETAILATTRMAP"/>
      <setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
                        columnName="JOURNALEVENTTYPEID"
                        remarks="Journal event type identifier"/>
      <setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
                        columnName="JOURNALEVENTDETAILATTRID"
                        remarks="Journal event detail attribute identifier"/>
      <setColumnRemarks tableName="AS_JOURNALEVENTDETAILATTRMAP"
                        columnName="LISTORDER"
                        remarks="Order in list"/>
   </changeSet>
</databaseChangeLog>

注意:问题出在元素setTableRemarks和属性remarks。它是从另一个元素复制而来的,而不是我想它应该是从 createTable 复制而来的。是我的模板有问题还是有其他问题?

用于此的 saxon 库是 9.8.0-14 我也尝试过 9.9.0-1 但没有任何改变。

而不是这样做...

<xsl:copy-of select="createTable//(@tableName,@remarks)" />

这样做...

<xsl:copy-of select="createTable/(@tableName,@remarks)" />

使用 // 是 shorthand 用于 /descendant-or-self::node()/,因此将搜索 createTable 的所有后代节点,而不仅仅是节点本身。由于这将 select 具有相同名称的多个属性,因此只会复制最后一个属性(将属性添加到已存在具有相同名称的现有属性的节点应该替换它)。