XSLT将属性添加到已处理的节点

时间:2018-06-26 14:07:50

标签: xslt saxon

这是示例xml:

var list = new List<string> { "1", "2", "3", "4" };
var count = list.Count;
listView1.BeginUpdate();
for (var i = 0; i < count / 2; i++)
    listView1.Items.Add(list[i]).SubItems.Add(list[count / 2 + i]);
listView1.EndUpdate();

这是模板:

<?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:comment>UNMATCHED</xsl:comment> <xsl:variable name="unmatchedChangeSets" select="changeSet[not(some $set in ($coreTablesVariable | $coreSequencesVariable | $coreIndexesVariable |$coreForeignConstraintsVariable |$coreViewsVariable) satisfies $set is .)]"/> <xsl:call-template name="createChangeLog"> <xsl:with-param name="outputFile" select="'unmatched-changes-changelog.xml'"/> <xsl:with-param name="changeLogContent" select="$unmatchedChangeSets"/> </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> 内部处理的输出xml添加到每个元素createChangelogTemplate的另一个属性(<changeSet>)中。我正在尝试添加另一个模板,该模板将context="legacy"与其他databaseChangelog/changeSet元素匹配,但对我不起作用。如果有一种方法可以在一个不错的地方做到这一点,因为我将需要准备更多的部分,例如xsl:attribute

添加模板后:

CORE

并将<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="$changeLogContent"/>,然后输出文件<xsl:apply-templates select="$changeLogContent" mode="legacy"/>可以,但是文件core-changelog.xml没有元素。

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

1 个答案:

答案 0 :(得分:0)

运行此命令时,在core-changelog.xml中获得changeSet 1和1-1,在unmatched-changelog.xml中获得changeSet 4。

如果您仍然无法在unmatched-changelog中看到任何内容,那么我们需要更详细地了解您的运行方式。

顺便说一句,可以选择不匹配的元素

argsort
相关问题