简体   繁体   English

使用XML从XML转换为CSV时CSV文件中的标题

[英]Title in CSV file when converting from XML to CSV using XML

I am trying to convert the XML to CSV using XSLT , I am getting the record but I need title in CSV file. 我正在尝试使用XSLT将XML转换为CSV,正在获取记录,但是我需要CSV文件中的标题。

Eg: 例如:

Name    EmailAddress
Gaurav   Gaur@yopmail.com
Satya    Satya@yopmail.com

XML XML

<?xml version="1.0"?>
<SearchResults>
<Columns>
    <Column Label="Name">Name</Column>
    <Column Label="Email Addresses">EmailAddress</Column>
</Columns>
<Rows>
    <Row>
        <Name>Gaurav Joshi</Name>
        <EmailAddress>ybenipohe-3888@yopmail.com</EmailAddress>

    </Row>
    <Row>
        <Name>Satya</Name>
        <EmailAddress>eqaddoxoqu-8703@yopmail.com</EmailAddress>
     </Row>

XSLT XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <!-- This transform is used to generate a CSV file.-->
  <xsl:output method="text" indent="no" />
  <xsl:template match="/">
    <xsl:apply-templates select="/SearchResults/Rows/Row" />
  </xsl:template>
  <xsl:template match="/SearchResults/Rows/Row">
    <xsl:text>&quot;</xsl:text>
    <xsl:value-of select="position()" />
    <xsl:text>&quot;</xsl:text>
    <xsl:for-each select="*">
      <xsl:text>,&quot;</xsl:text>
      <xsl:value-of select="." />
      <xsl:text>&quot;</xsl:text>
    </xsl:for-each>
    <xsl:text>
    </xsl:text>
  </xsl:template>
  <xsl:template match="/SearchResults/Columns" />
</xsl:stylesheet>

You currently have a template matching /SearchResults/Columns , which is actually redundant at the moment, because you don't have any templates that explicitly select these (Due to you doing <xsl:apply-templates select="/SearchResults/Rows/Row" /> ). 当前,您有一个匹配/SearchResults/Columns的模板,此刻实际上是多余的,因为您没有任何显式选择这些模板的模板(由于您执行<xsl:apply-templates select="/SearchResults/Rows/Row" /> )。

But you can easily adapt it to output the headings, and then you would just need an xsl:apply-templates to select it. 但是您可以轻松调整它以输出标题,然后只需要一个xsl:apply-templates即可选择它。

Try this XSLT 试试这个XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <!-- This transform is used to generate a CSV file.-->
  <xsl:output method="text" indent="no" />

  <xsl:template match="/">
    <xsl:apply-templates select="/SearchResults/Columns" />
    <xsl:apply-templates select="/SearchResults/Rows/Row" />
  </xsl:template>

  <xsl:template match="Row">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="position()" />
    <xsl:text>"</xsl:text>
    <xsl:for-each select="*">
      <xsl:text>,"</xsl:text>
      <xsl:value-of select="." />
      <xsl:text>"</xsl:text>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="Columns">
    <xsl:text>"Row"</xsl:text>
    <xsl:for-each select="Column">
       <xsl:text>,"</xsl:text>
       <xsl:value-of select="@Label" />
       <xsl:text>"</xsl:text>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

But this makes the (big?) assumption that the child nodes of each Row are in the same order as the relevant Column nodes. 但这(大)假设是,每个Row的子节点与相关Column节点的顺序相同。 If this is not the case, you have work to do. 如果不是这种情况,则您有工作要做。 Try replacing the template matching Row with this one instead 尝试用此模板替换匹配Row的模板

  <xsl:template match="Row">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="position()" />
    <xsl:text>"</xsl:text>
    <xsl:variable name="Row" select="." />
    <xsl:for-each select="/SearchResults/Columns/Column">
      <xsl:text>,"</xsl:text>
      <xsl:value-of select="$Row/*[name() = current()]" />
      <xsl:text>"</xsl:text>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM