代码之家  ›  专栏  ›  技术社区  ›  bilak

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

  •  0
  • bilak  · 技术社区  · 6 年前

    我正在使用此模板:

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

    注意:问题出在元素settablemarks和属性marks中。它是从另一个元素而不是createTable中复制的。我的模板有什么问题吗?还是有其他问题?

    9.8.0-14 9.9.0-1 但一切都没有改变。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Tim C    6 年前

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

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

    使用 // 是的缩写 /descendant-or-self::node()/ createTable ,而不仅仅是节点本身。由于这将选择具有相同名称的多个属性,因此只复制最后一个属性(因为将属性添加到已存在具有相同名称的现有属性的节点时,应该将其替换)。