Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CSV到XML的修改(XSLT?)_Xml_Xslt_Csv - Fatal编程技术网

CSV到XML的修改(XSLT?)

CSV到XML的修改(XSLT?),xml,xslt,csv,Xml,Xslt,Csv,我一直在论坛上搜索我的问题的答案,没有任何运气。我希望你能帮助我。我有一个简单的CSV文件,我需要将其转换为XML(这一部分很简单),但我需要修改它,使其包含子元素。例如: 我所拥有的: <Unit> <UnitID>K000009107</UnitID> <DateLastModified>2003-06-23</DateLastModified> <Family>SAPOT

我一直在论坛上搜索我的问题的答案,没有任何运气。我希望你能帮助我。我有一个简单的CSV文件,我需要将其转换为XML(这一部分很简单),但我需要修改它,使其包含子元素。例如:

我所拥有的:

<Unit>
        <UnitID>K000009107</UnitID>
        <DateLastModified>2003-06-23</DateLastModified>
        <Family>SAPOTACEAE</Family>
        <Genus>Pouteria</Genus>
        <Species>ferrugineo-tomentos</Species>
        <Identifier>Smith, J</Identifier>
        <StartMonth>05</StartMonth>
        <StartYear>1997</StartYear>
        <TypeStatus>Type</TypeStatus>
</Unit>

K000009107
2003-06-23
皂荚科
山榄属
铁锈新绒毛
史密斯,J
05
1997
类型
我需要的是:

<Unit>
    <UnitID>K000009107</UnitID>
    <DateLastModified>2003-06-23</DateLastModified>
    <Identification StoredUnderName="true">
        <Family>SAPOTACEAE</Family>
        <Genus>Pouteria</Genus>
        <Species>ferrugineo-tomentos</Species>
        <Identifier>Smith, J</Identifier>
        <IdentificationDate>
            <StartMonth>05</StartMonth>
            <StartYear>1997</StartYear>
        </IdentificationDate>
    <TypeStatus>Type</TypeStatus>
    </Identification>
</Unit>

K000009107
2003-06-23
皂荚科
山榄属
铁锈新绒毛
史密斯,J
05
1997
类型

我需要在一个大数据集上进行修改。我猜XSLT可以完成这项工作,但我不知道这是如何工作的。有什么想法吗?

这里有一种方法:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/*">
  <xsl:copy>
   <xsl:apply-templates select="UnitID|DateLastModified"/>
   <Identification StoredUnderName="true">
    <xsl:apply-templates select=
     "*[not(contains('|UnitID|DateLastModified|StartMonth|StartYear|TypeStatus|',
                    concat('|',name(),'|')))]"/>
    <IdentificationDate>
     <xsl:apply-templates select="StartMonth|StartYear"/>
    </IdentificationDate>
    <xsl:apply-templates select="TypeStatus"/>
   </Identification>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<Unit>
        <UnitID>K000009107</UnitID>
        <DateLastModified>2003-06-23</DateLastModified>
        <Family>SAPOTACEAE</Family>
        <Genus>Pouteria</Genus>
        <Species>ferrugineo-tomentos</Species>
        <Identifier>Smith, J</Identifier>
        <StartMonth>05</StartMonth>
        <StartYear>1997</StartYear>
        <TypeStatus>Type</TypeStatus>
</Unit>
<Unit>
   <UnitID>K000009107</UnitID>
   <DateLastModified>2003-06-23</DateLastModified>
   <Identification StoredUnderName="true">
      <Family>SAPOTACEAE</Family>
      <Genus>Pouteria</Genus>
      <Species>ferrugineo-tomentos</Species>
      <Identifier>Smith, J</Identifier>
      <IdentificationDate>
         <StartMonth>05</StartMonth>
         <StartYear>1997</StartYear>
      </IdentificationDate>
      <TypeStatus>Type</TypeStatus>
   </Identification>
</Unit>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <xsl:copy>
   <xsl:copy-of select="UnitID|DateLastModified"/>
   <Identification StoredUnderName="true">
    <xsl:copy-of select=
     "*[not(contains('|UnitID|DateLastModified|StartMonth|StartYear|TypeStatus|',
                    concat('|',name(),'|')))]"/>
    <IdentificationDate>
     <xsl:copy-of select="StartMonth|StartYear"/>
    </IdentificationDate>
    <xsl:copy-of select="TypeStatus"/>
   </Identification>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

K000009107
2003-06-23
皂荚科
山榄属
铁锈新绒毛
史密斯,J
05
1997
类型
生成所需的正确结果:

<Unit>
        <UnitID>K000009107</UnitID>
        <DateLastModified>2003-06-23</DateLastModified>
        <Family>SAPOTACEAE</Family>
        <Genus>Pouteria</Genus>
        <Species>ferrugineo-tomentos</Species>
        <Identifier>Smith, J</Identifier>
        <StartMonth>05</StartMonth>
        <StartYear>1997</StartYear>
        <TypeStatus>Type</TypeStatus>
</Unit>
<Unit>
   <UnitID>K000009107</UnitID>
   <DateLastModified>2003-06-23</DateLastModified>
   <Identification StoredUnderName="true">
      <Family>SAPOTACEAE</Family>
      <Genus>Pouteria</Genus>
      <Species>ferrugineo-tomentos</Species>
      <Identifier>Smith, J</Identifier>
      <IdentificationDate>
         <StartMonth>05</StartMonth>
         <StartYear>1997</StartYear>
      </IdentificationDate>
      <TypeStatus>Type</TypeStatus>
   </Identification>
</Unit>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <xsl:copy>
   <xsl:copy-of select="UnitID|DateLastModified"/>
   <Identification StoredUnderName="true">
    <xsl:copy-of select=
     "*[not(contains('|UnitID|DateLastModified|StartMonth|StartYear|TypeStatus|',
                    concat('|',name(),'|')))]"/>
    <IdentificationDate>
     <xsl:copy-of select="StartMonth|StartYear"/>
    </IdentificationDate>
    <xsl:copy-of select="TypeStatus"/>
   </Identification>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

K000009107
2003-06-23
皂荚科
山榄属
铁锈新绒毛
史密斯,J
05
1997
类型
此转换可以稍微缩短,但会失去灵活性:

<Unit>
        <UnitID>K000009107</UnitID>
        <DateLastModified>2003-06-23</DateLastModified>
        <Family>SAPOTACEAE</Family>
        <Genus>Pouteria</Genus>
        <Species>ferrugineo-tomentos</Species>
        <Identifier>Smith, J</Identifier>
        <StartMonth>05</StartMonth>
        <StartYear>1997</StartYear>
        <TypeStatus>Type</TypeStatus>
</Unit>
<Unit>
   <UnitID>K000009107</UnitID>
   <DateLastModified>2003-06-23</DateLastModified>
   <Identification StoredUnderName="true">
      <Family>SAPOTACEAE</Family>
      <Genus>Pouteria</Genus>
      <Species>ferrugineo-tomentos</Species>
      <Identifier>Smith, J</Identifier>
      <IdentificationDate>
         <StartMonth>05</StartMonth>
         <StartYear>1997</StartYear>
      </IdentificationDate>
      <TypeStatus>Type</TypeStatus>
   </Identification>
</Unit>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <xsl:copy>
   <xsl:copy-of select="UnitID|DateLastModified"/>
   <Identification StoredUnderName="true">
    <xsl:copy-of select=
     "*[not(contains('|UnitID|DateLastModified|StartMonth|StartYear|TypeStatus|',
                    concat('|',name(),'|')))]"/>
    <IdentificationDate>
     <xsl:copy-of select="StartMonth|StartYear"/>
    </IdentificationDate>
    <xsl:copy-of select="TypeStatus"/>
   </Identification>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

您使用的XSLT版本是什么?如果您使用的是2.0,并且可以发布一个CSV示例,那么您可以在一个样式表中完成整个工作(转换和嵌套)。