首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XSL将XML转换为CSV的repeat子节点出现问题

使用XSL将XML转换为CSV的repeat子节点出现问题
EN

Stack Overflow用户
提问于 2021-04-20 20:54:17
回答 1查看 19关注 0票数 1

我正在进行XML文件到CSV文件的转换。

查看XML的一部分:

代码语言:javascript
复制
<bota_form_v_20200610 xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:orx="http://openrosa.org/xforms" xmlns:odk="http://www.opendatakit.org/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="fm_bota_2020" version="2020061000">
<start>2021-04-20T09:27:24.295+02:00</start>
<end>2021-04-20T10:18:51.038+02:00</end>
<deviceid>collect:JZUeuhzIlUe38qIl</deviceid>
<parcelle>
<identifier>Ab</identifier>
<sample_date>2021-04-20</sample_date>
<proto>farmland</proto>
<yes_no_parcelle_hza>no</yes_no_parcelle_hza>
<md_parcelle>18710002</md_parcelle>
<md_carre/>
<md_hauteur_vegetation>25</md_hauteur_vegetation>
<search_text_culture>Blé</search_text_culture>
<md_culture>Blé</md_culture>
<ocs>41</ocs>
</parcelle>
<quadrat_repeat>
<yes_no_md_numero_waypoint>no</yes_no_md_numero_waypoint>
<nom_gps>T12</nom_gps>
<md_numero_waypoint>264</md_numero_waypoint>
<md_sampling>in</md_sampling>
<point_sampling>in1</point_sampling>
<type_bordure>haie</type_bordure>
<zone>C1-H0-N0</zone>
<md_densite>12</md_densite>
<sous_quadrat_repeat_count>4</sous_quadrat_repeat_count>
<sous_quadrat_repeat>
<md_subquadrat>a</md_subquadrat>
<yes_no_diff_esp_plt>yes</yes_no_diff_esp_plt>
<esp_plt_rpt>
<search_text_plante_latin_rpt>Vero</search_text_plante_latin_rpt>
<md_plante_latin_rpt>Veronica-hederifolia</md_plante_latin_rpt>
<md_code_bayer_rpt>VERHE</md_code_bayer_rpt>
<md_abondance>2</md_abondance>
<md_stade_pheno>v</md_stade_pheno>
</esp_plt_rpt>

但是有许多“quadrat_repeat”节点,每个节点都有许多“sous_quadrat_repeat”节点,并且每个节点都有一些“esp_plt_rpt”节点。

对于每个'esp_plt_rpt‘子节点,我需要一行。这行代码中的每一行都需要拥有其父元素的所有属性( 'sous_quadrat_repeat','quadra_repeat‘和parcelle’的属性)。如下所示:

代码语言:javascript
复制
Attr_Parc1    Attr_Parc2    Quadra_repeat1 ...   quadra_repeatN ... sous_quadrat_repeatN

下面是我的XSL (1.0):

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="UTF-8" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:variable name="sep" select="';'" />
  <xsl:variable name="nl" select="'&#xa;'" />

  <xsl:template match="bota_form_v_20200610">
<!--    <xsl:apply-templates select="quadrat_repeat/sous_quadrat_repeat"/> 
-->
<xsl:apply-templates select="quadrat_repeat"/> 
 </xsl:template>

  <xsl:template match="esp_plt_rpt">
    <xsl:value-of select="md_abondance" />
    <xsl:value-of select="$nl" />
  </xsl:template>




  <xsl:template match="sous_quadrat_repeat">
    <xsl:variable name="tmp">
      <xsl:value-of select="ancestor::bota_form_v_20200610/@id" />
      <xsl:value-of select="$sep" />
      <xsl:value-of select="ancestor::bota_form_v_20200610/quadrat_repeat/nom_gps" />
      <xsl:value-of select="$sep" />
      <xsl:value-of select="ancestor::bota_form_v_20200610/quadrat_repeat/md_sampling" />
      <xsl:value-of select="$sep" />
      <xsl:value-of select="ancestor::bota_form_v_20200610/quadrat_repeat/zone" />
      <xsl:value-of select="$sep" />

      <xsl:value-of select="ancestor::bota_form_v_20200610/quadrat_repeat/md_densite"/>
      <xsl:value-of select="$sep" />
    </xsl:variable>


    <xsl:for-each select="ancestor::bota_form_v_20200610/quadrat_repeat/sous_quadrat_repeat/esp_plt_rpt">
      <xsl:copy-of select="$tmp"/>

      <xsl:value-of select="ancestor::bota_form_v_20200610/quadrat_repeat/sous_quadrat_repeat/md_subquadrat"/>
      <xsl:value-of select="$sep" />
      <xsl:value-of select="md_plante_latin_rpt" />
      <xsl:value-of select="$sep" />
      <xsl:value-of select="md_stade_pheno" />
      <xsl:value-of select="$sep" />

      <xsl:apply-templates select="." />
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

此代码在转到新的esp_pt_rpt时不接受新的“sous_quadrat_repeat”。因此,我的所有行都具有第一个sous_quadrat_repeat节点的属性。

EN

回答 1

Stack Overflow用户

发布于 2021-06-08 19:48:28

您的xslt离我们不远了。将可重复数据存储到变量中的好方法。我稍微调整了一下:

代码语言:javascript
复制
<?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" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:variable name="sep" select="';'" />
  <xsl:variable name="nl" select="'&#xa;'" />
  
  <xsl:template match="bota_form_v_20200610">
    <xsl:apply-templates select="quadrat_repeat/sous_quadrat_repeat"/> 
  </xsl:template>
   
  <xsl:template match="sous_quadrat_repeat">
    <xsl:variable name="tmp">
      <xsl:value-of select="ancestor::bota_form_v_20200610/@id" />
      <xsl:value-of select="$sep" />
      <xsl:value-of select="parent::quadrat_repeat/nom_gps" />
      <xsl:value-of select="$sep" />
      <xsl:value-of select="parent::quadrat_repeat/md_sampling" />
      <xsl:value-of select="$sep" />
      <xsl:value-of select="parent::quadrat_repeat/zone" />
      <xsl:value-of select="$sep" />
      <xsl:value-of select="parent::quadrat_repeat/md_densite"/>
      <xsl:value-of select="$sep" />
      <xsl:value-of select="md_subquadrat"/>
      <xsl:value-of select="$sep" />
    </xsl:variable>    
    
    <xsl:for-each select="esp_plt_rpt">
      <xsl:value-of select="$tmp"/>
      <xsl:value-of select="md_plante_latin_rpt" />
      <xsl:value-of select="$sep" />
      <xsl:value-of select="md_stade_pheno" />
      <xsl:value-of select="$sep" />
      <xsl:value-of select="md_abondance" />
      <xsl:value-of select="$nl" />
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67179367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档