簡體   English   中英

將xml轉換為csv時如何僅選擇所需的字段和值

[英]How to select only needed fields and values when transform xml to csv

我是xml xslt transfrmation中的新手。 請幫我拿一個小樣本以供理解。 我想:1)將xml轉換為csv 2)第一個字符串必須包含列名稱3)僅轉換字段:名稱,狀態,記錄\\記錄(僅獲取記錄列表中的最后一條記錄)

我也有xslt代碼,但是我無法根據需要修改此代碼。 請幫我修改代碼。

CSV

name, state, record 
"Shockwave", "New", "3"
"Other", " Canceled ", "7"

XML文件

<projects>
  <project>
   <name>Shockwave</name> 
   <language>Ruby</language> 
   <owner>Brian May</owner> 
   <state>New</state> 
   <startDate>31/10/2008 0:00:00</startDate> 
   <records>
      <record>1</records>
      <record>5</records>
      <record>3</records>
   </records>
  </project>
  <project>
   <name>Other</name> 
   <language>Erlang</language> 
   <owner>Takashi Miike</owner> 
   <state> Canceled </state> 
   <startDate>07/11/2008 0:00:00</startDate> 
   <records>
      <record>5</records>
      <record>6</records>
      <record>7</records>
   </records>
  </project>
</projects>



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

  <xsl:param name="delim" select="','" />
  <xsl:param name="quote" select="'&quot;'" />
  <xsl:param name="break" select="'&#xA;'" />

  <xsl:template match="/">
    <xsl:apply-templates select="projects/project" />
  </xsl:template>

  <xsl:template match="project">
    <xsl:apply-templates />
    <xsl:if test="following-sibling::*">
      <xsl:value-of select="$break" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="*">
    <!-- remove normalize-space() if you want keep white-space at it is --> 
    <xsl:value-of select="concat($quote, normalize-space(), $quote)" />
    <xsl:if test="following-sibling::*">
      <xsl:value-of select="$delim" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="text()" />
</xsl:stylesheet>

我認為您正在使事情變得更加復雜。 嘗試這種方式:

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="/projects">
    <xsl:text>name, state, record&#10;</xsl:text>
    <xsl:for-each select="project">
        <xsl:value-of select="concat('&quot;', name, '&quot;')" />
        <xsl:text>, </xsl:text>
        <xsl:value-of select="concat('&quot;', state, '&quot;')" />
        <xsl:text>, </xsl:text>
        <xsl:value-of select="concat('&quot;', records/record[last()], '&quot;')" />
        <xsl:if test="position()!=last()">
            <xsl:text>&#10;</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

請注意,這假定所有涉及的字段都不包含引號。


添加:

您可以修改xslt文件並添加函數,以單引號將兩個double替換為“名稱”字段的值嗎?

將此模板添加到樣式表:

<xsl:template name="replace">
    <xsl:param name="text"/>
    <xsl:param name="searchString">"</xsl:param>
    <xsl:param name="replaceString">""</xsl:param>
    <xsl:choose>
        <xsl:when test="contains($text,$searchString)">
            <xsl:value-of select="substring-before($text,$searchString)"/>
            <xsl:value-of select="$replaceString"/>
            <xsl:call-template name="replace">
                <xsl:with-param name="text" select="substring-after($text,$searchString)"/>
                <xsl:with-param name="searchString" select="$searchString"/>
                <xsl:with-param name="replaceString" select="$replaceString"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

並更改此:

        <xsl:value-of select="concat('&quot;', name, '&quot;')" />
        <xsl:text>, </xsl:text>

至:

        <xsl:text>"</xsl:text>
        <xsl:call-template name="replace">
            <xsl:with-param name="text" select="name"/>
        </xsl:call-template>
        <xsl:text>",</xsl:text>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM