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

数组的xslt concat值

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

    我想应用模板,其中指定的元素包含前缀为某个常量的数组值。

    <xsl:variable name="coreTables"
                  select="('TAB1', 'TAB2')" />
    <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">
        <xsl:comment> CORE TABLES </xsl:comment>
        <xsl:apply-templates select="changeSet[createTable/@tableName=$coreTables]"/>
        <xsl:comment> CORE SEQUENCES </xsl:comment>
        <xsl:apply-templates select="changeSet[createSequence/@sequenceName='SEQ_'[$coreTables]]"/>
    </xsl:template>
    

    这是XML示例:

    <?xml version="1.1" encoding="UTF-8" standalone="no"?>
    <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="TAB1">
                <column></column>
            </createTable>
        </changeSet>
    
        <changeSet id="1-1" author="a">
            <createSequence sequenceName="SEQ_TAB1" />
        </changeSet>
        <changeSet id="4" author="A">
            <createTable tableName="TAB4">
                <column></column>
            </createTable>
        </changeSet>
    </databaseChangeLog>
    

    所以最后一个 apply-templates 我想匹配的所有节点 createSequence Where属性 sequenceName SEQ_ + value of some coreTables .但我不知道如何写这个选择,或者是否可能这样。

    我使用的是XSLT2和Saxon9.8he。

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

    有很多方法可以做到这一点。这是一对…

    <xsl:apply-templates 
         select="changeSet[createSequence/@sequenceName = (for $i in $coreTables return concat('SEQ_', $i))]"/>
    
    <xsl:apply-templates 
         select="changeSet[createSequence[starts-with(@sequenceName, 'SEQ_') and substring-after(@sequenceName, 'SEQ_') = $coreTables]]"/>