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

使用XSLT实现含逗号数据的XML转CSV方案问询

Got it, let's sort out your XML-to-CSV conversion problem! The key here is twofold: avoiding comma-related formatting breaks in your CSV, and correctly packaging the x/y/z values into that [xValue,yValue,zValue] field.

First, let's assume your sample XML looks something like this (adjust if your structure differs):

<root>
  <row rowId="1">
    <data1>Test Data No Comma</data1>
    <xValue>15</xValue>
    <yValue>25</yValue>
    <zValue>35</zValue>
  </row>
  <row rowId="2">
    <data1>Data with, a comma in it</data1>
    <xValue>45</xValue>
    <yValue>55</yValue>
    <zValue>65</zValue>
  </row>
</root>

Here's a revised XSLT that will generate exactly the CSV format you want, while handling commas in your data properly (following standard CSV rules):

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

  <!-- Output CSV header first -->
  <xsl:template match="/">
    <xsl:text>rowId,data 1,coordinates&#10;</xsl:text>
    <xsl:apply-templates select="root/row"/>
  </xsl:template>

  <!-- Process each row element -->
  <xsl:template match="row">
    <!-- Print rowId -->
    <xsl:value-of select="@rowId"/>
    <xsl:text>,</xsl:text>

    <!-- Handle data1 field: wrap in quotes if it contains commas -->
    <xsl:choose>
      <xsl:when test="contains(data1, ',')">
        <xsl:text>"</xsl:text>
        <xsl:value-of select="data1"/>
        <xsl:text>"</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="data1"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>,</xsl:text>

    <!-- Build the [x,y,z] coordinate field -->
    <xsl:text>[</xsl:text>
    <xsl:value-of select="xValue"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="yValue"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="zValue"/>
    <xsl:text>]</xsl:text>

    <!-- Add a new line after each row -->
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

Key details about this solution:

  • CSV Compliance: Any data 1 value with a comma gets wrapped in double quotes (standard CSV behavior), so parsers won't mistake those commas for field separators.
  • Coordinate Packaging: The x/y/z values are concatenated into a single [x,y,z] string, which becomes one CSV field—so the commas inside the brackets won't break your column structure.
  • Clean Output: Uses &#10; for proper line breaks across systems, and sets UTF-8 encoding to avoid character issues.

When you run this transformation, you'll get output like this:

rowId,data 1,coordinates
1,Test Data No Comma,[15,25,35]
2,"Data with, a comma in it",[45,55,65]

Quick adjustments if your XML is different:

  • If your rows are under a different parent element (not <root>), update the select="root/row" path to match your actual structure.
  • If your data field isn't named <data1>, swap data1 with your real field name.
  • If you need to handle double quotes in your data (e.g., data1="He said ""hello"""), add a replace step (for XSLT 2.0+ use <xsl:value-of select="replace(data1, '&quot;', '&quot;&quot;')"/>, or use a recursive template for XSLT 1.0).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:55:32