来自外部文档的xslt复制属性

时间:2018-10-05 14:48:42

标签: xslt saxon

我正在关注xml

<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="c529c6ea-45c2-4ec2-8c9d-7bc935434d21" author="system">
      <setTableRemarks remarks="this is wrong"
                       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>

和名称为fixedremarks.xml的文档完全相同,但<setTableRemarks remarks="this is ok" tableName="AS_JOURNALEVENTDETAILATTRMAP"/>没什么变化

使用以下模板,我试图在remarks内修复属性setTableRemarks,但没有成功-我不知道如何从外部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">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <xsl:variable name="originalChangeLog" select="document('/tmp/fixedremarks.xml')"/>

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

    <xsl:key name="remarkTableName" match="setTableRemarks" use="@tableName"/>

    <xsl:template match="changeSet[setTableRemarks]">
        <xsl:variable name="currentRemarkTable" select="setTableRemarks/@tableName"/>
        <xsl:comment select="$currentRemarkTable"/>
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:element name="setTableRemarks">

                <xsl:attribute name="remarks"
                               select="$originalChangeLog/key('remarkTableName', $currentRemarkTable)"/>
                <xsl:attribute name="tableName" select="setTableRemarks/@tableName"/>
            </xsl:element>
            <xsl:copy-of select="*[not(self::setTableRemarks)]"/>
        </xsl:copy>

    </xsl:template>

</xsl:transform>

有人可以告诉我如何正确地映射外部文档中的注释吗?

1 个答案:

答案 0 :(得分:1)

我认为您只希望拥有自己的身份转换模板

<xsl:template match="changeSet/setTableRemarks[key('remarkTableName', @tableName, $originalChangeLog)]/@remarks">
  <xsl:attribute name="{name()}" select="key('remarkTableName', ../@tableName, $originalChangeLog)/@remarks"/>
</xsl:template>

https://xsltfiddle.liberty-development.net/6qVRKwR有一个在线示例(对于独立的示例,辅助XML内联为变量,但是如果您保留<xsl:variable name="originalChangeLog" select="document('/tmp/fixedremarks.xml')"/>,它也将正常工作。