【问题标题】:XSLT creation from XML and load into CSV file从 XML 创建 XSLT 并加载到 CSV 文件中
【发布时间】:2021-03-02 11:09:43
【问题描述】:

我需要将 XML 文件转换为 CSV。

预期输出:

tag1  tag2   Typearray
Name1 Name2     1
Name1 Name2     13

使用 XSLT 只能获得一条记录,而数组中的第二条记录被拒绝。

tag1,tag2,CommOffer,TypeArray
name1,name2,1

您能告诉我如何使用 XSLT 读取数组值吗?

示例 XML:

    <Master2>
       <Child1>
          <tag1>name1</tag1>
          <tag2>name2</tag2>
          <TypeArray>
             <value>1</value>
             <value>13</value>
          </TypeArray>     
      </Child1>
    </Master2>
</Master1>

XSLT:

  <xsl:template match="/"> 
      <xsl:text>tag1,tag2,TypeArray</xsl:text> 
         <xsl:text>&#xA;</xsl:text> 
         <xsl:for-each select="Master1/Master2/Child1">
             <xsl:call-template name="CsvEscape"><xsl:with-param name="value" select="normalize- 
              space(tag1)"/></xsl:call-template>
             <xsl:text>,</xsl:text>
             <xsl:call-template name="CsvEscape"><xsl:with-param name="value" select="normalize- 
              space(tag2)"/></xsl:call-template>
             <xsl:text>,</xsl:text>
    <xsl:call-template name="CsvEscape"><xsl:with-param name="value" select="TypeArray/value"/> 
         <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
 </xsl:template> 

【问题讨论】:

  • 你标记了这个sas - 你在用SAS写这个吗?
  • 是的,我在 sas 9.4M2 中使用 PROC XSL

标签: xml xslt sas


【解决方案1】:

很难从单个示例中推断出所需的逻辑。如果 - 看起来 - 您想为每个 TypeArray/value 创建一行,并在每一行中重复 tag1tag2 值,您可以执行以下操作:

XSLT 1.0

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

<xsl:template match="/Master1">
    <!-- header -->
    <xsl:text>tag1,tag2,TypeArray&#10;</xsl:text>
    <!-- data -->
    <xsl:for-each select="Master2/Child1">
        <xsl:variable name="tags">
            <xsl:value-of select="tag1"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="tag2"/>
            <xsl:text>,</xsl:text>
        </xsl:variable>
        <xsl:for-each select="TypeArray/value">
            <xsl:value-of select="$tags"/>
            <xsl:value-of select="."/>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    相关资源
    最近更新 更多