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

时间:2018-10-05 09:49:51

标签: xslt saxon

我正在使用此模板:

this

像这样转换xml:

<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 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>

注意:问题出在元素setTableRemarks和属性备注中。它是从另一个元素而不是从createTable复制而来的,正如我想的那样。我的模板中的内容有误还是存在其他问题?

用于此目的的

saxon库是<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> ,我也尝试过使用9.8.0-14,但没有任何改变。

1 个答案:

答案 0 :(得分:0)

而不是这样做...

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

执行此操作...

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

使用///descendant-or-self::node()/的简写,因此将搜索createTable的所有后代节点,而不仅仅是节点本身。因为这将选择多个具有相同名称的属性,所以仅将复制最后一个(将属性添加到已经存在相同名称的现有节点的节点上,则将其替换)。