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

使用XSLT扁平化复杂XML的技术问询(附指定XML结构)

嘿,我来帮你搞定这个XML扁平化的需求!根据你给出的XML结构,我写了一个XSLT 1.0的样式表,能把嵌套的层级结构拉平,让每个Valuation节点对应一条包含所有关联字段的扁平化记录,同时保留上层Property节点的全局属性。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <!-- 匹配根节点,启动处理流程 -->
    <xsl:template match="/Root">
        <FlatProperties>
            <!-- 遍历每个Property下的Valuation节点,每个节点生成一条扁平化记录 -->
            <xsl:for-each select="Property/Valuation">
                <FlatProperty>
                    <!-- 提取上层Property节点的全局字段 -->
                    <CollectionID><xsl:value-of select="../CollectionID"/></CollectionID>
                    <CollectionTypeCode><xsl:value-of select="../CollectionTypeCode"/></CollectionTypeCode>
                    <!-- 提取Valuation下的PropertyReference ID -->
                    <PropertyReferenceID><xsl:value-of select="PropertyReference/ID"/></PropertyReferenceID>
                    <!-- 提取ValueGroup深层的名称字段 -->
                    <PropertyValueName><xsl:value-of select="ValueGroup/PropertyValue/NameSpecification/Name"/></PropertyValueName>
                </FlatProperty>
            </xsl:for-each>
        </FlatProperties>
    </xsl:template>
</xsl:stylesheet>

关键逻辑拆解

  • 根节点入口:从/Root开始,创建FlatProperties作为输出容器,统一存放所有扁平化后的记录。
  • 遍历核心节点:用xsl:for-each循环处理每个Property下的Valuation——这是扁平化的核心,因为每个Valuation就是一条独立的业务条目。
  • 跨层级取值:在Valuation的上下文里,用../回到父节点Property,就能拿到CollectionID这类全局字段;直接遍历当前Valuation下的嵌套节点,把深层的ID和Name提取到同一层级。

输入输出示例

简化版输入XML

<Root>
    <Property>
        <CollectionID>ESTATE_APPRAISAL</CollectionID>
        <CollectionTypeCode>003</CollectionTypeCode>
        <Valuation>
            <PropertyReference>
                <ID>CONTRIBUTIONFACTOR</ID>
            </PropertyReference>
            <ValueGroup>
                <PropertyValue>
                    <NameSpecification>
                        <Name>CF 4 - General Purpose Asset</Name>
                    </NameSpecification>
                </PropertyValue>
            </ValueGroup>
        </Valuation>
        <Valuation>
            <PropertyReference>
                <ID>REPLACEMENTCOST</ID>
            </PropertyReference>
            <ValueGroup>
                <PropertyValue>
                    <NameSpecification>
                        <Name>RC - Building Structure</Name>
                    </NameSpecification>
                </PropertyValue>
            </ValueGroup>
        </Valuation>
    </Property>
</Root>

扁平化输出XML

<FlatProperties>
    <FlatProperty>
        <CollectionID>ESTATE_APPRAISAL</CollectionID>
        <CollectionTypeCode>003</CollectionTypeCode>
        <PropertyReferenceID>CONTRIBUTIONFACTOR</PropertyReferenceID>
        <PropertyValueName>CF 4 - General Purpose Asset</PropertyValueName>
    </FlatProperty>
    <FlatProperty>
        <CollectionID>ESTATE_APPRAISAL</CollectionID>
        <CollectionTypeCode>003</CollectionTypeCode>
        <PropertyReferenceID>REPLACEMENTCOST</PropertyReferenceID>
        <PropertyValueName>RC - Building Structure</PropertyValueName>
    </FlatProperty>
</FlatProperties>

拓展:输出CSV格式(适合表格工具)

如果你想要直接生成可导入Excel的CSV,只需要调整输出方式:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8"/>

    <xsl:template match="/Root">
        <!-- 输出CSV表头 -->
        <xsl:text>CollectionID,CollectionTypeCode,PropertyReferenceID,PropertyValueName&#10;</xsl:text>
        <xsl:for-each select="Property/Valuation">
            <!-- 按逗号分隔字段,若字段含逗号可自行添加引号包裹逻辑 -->
            <xsl:value-of select="../CollectionID"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="../CollectionTypeCode"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="PropertyReference/ID"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="ValueGroup/PropertyValue/NameSpecification/Name"/>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:56:46