You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XSL将带动态列的Liquibase XML转换为CSV格式

实现Liquibase XML动态转CSV的XSLT方案

核心实现逻辑

  • 优先适配Liquibase XML的默认命名空间,确保能正确匹配到所有节点
  • 动态生成表头:取第一个insert节点下所有column的name属性作为CSV表头,无需硬编码列名
  • 按表头顺序逐行取值:每个insert节点对应一行CSV数据,自动匹配对应字段的取值(数值、字符串、布尔值分别处理,布尔值统一转为大写格式)
  • 自动适配不同XML的列结构,列属性变动时不需要修改转换脚本

可直接使用的XSLT转换脚本

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:lb="http://www.liquibase.org/xml/ns/dbchangelog"
                exclude-result-prefixes="lb">
    <xsl:output method="text" encoding="UTF-8" omit-xml-declaration="yes"/>

    <!-- 存储所有列名,方便后续按顺序取值 -->
    <xsl:variable name="columns" select="/*/lb:Tester/lb:insert[1]/lb:column/@name"/>

    <xsl:template match="/">
        <!-- 输出表头 -->
        <xsl:for-each select="$columns">
            <xsl:value-of select="."/>
            <xsl:if test="position() != last()">,</xsl:if>
        </xsl:for-each>
        <xsl:text>&#10;</xsl:text>

        <!-- 逐行输出每条插入数据 -->
        <xsl:for-each select="/*/lb:Tester/lb:insert">
            <xsl:variable name="currentInsert" select="."/>
            <xsl:for-each select="$columns">
                <xsl:variable name="colName" select="."/>
                <xsl:variable name="currentCol" select="$currentInsert/lb:column[@name=$colName]"/>
                <!-- 优先取对应类型的属性值 -->
                <xsl:choose>
                    <xsl:when test="$currentCol/@valueBoolean">
                        <xsl:value-of select="upper-case($currentCol/@valueBoolean)"/>
                    </xsl:when>
                    <xsl:when test="$currentCol/@valueNumeric">
                        <xsl:value-of select="$currentCol/@valueNumeric"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$currentCol/@value"/>
                    </xsl:otherwise>
                </xsl:choose>
                <xsl:if test="position() != last()">,</xsl:if>
            </xsl:for-each>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

使用方法

使用任意支持XSLT 1.0及以上版本的转换工具(如xsltproc、Saxon、JDK内置的XSLT转换工具)执行转换即可,命令示例:

xsltproc transform.xsl your_liquibase_file.xml > output.csv

内容的提问来源于stack exchange,提问作者kathir43

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 18:15:01