简体   繁体   English

通过xsl(msxsl)进行xml转换

[英]xml transformation via xsl (msxsl)

I need to convert a xml-file to a csv-file. 我需要将xml文件转换为csv文件。 Therefore I'm using msxsl. 因此,我正在使用msxsl。 The xml-file looks like this: xml文件如下所示:

(...)
<Test>
  <Field1>Blabla</Field1>
  <Values>
    <Year>2012</Year> 
    <Value>12</Value>
  </Values>
  <Values>
    <Year>2013</Year> 
    <Value>1234</Value>
  </Values>
  <Field2>abc</Field2>
</Test>

<Test>
  <Field1>Blubblub</Field1>
  <Field2>def</Field2>
</Test>
(...)

Which means: There are data sets "Test" with several nodes "Values", and others without any. 这意味着:存在带有多个“值”节点的“测试”数据集,而没有任何节点的数据集。

I tried to solve this with for-each: 我试图用for-each解决这个问题:

(...)
<xsl:for-each select="Test">
(...)
<xsl:for-each select="Values">
(...)
</xsl:for-each>
</xsl:for-each>
(...)

This works (in a way), but the number of colums does not match between the data sets. 这可以(以某种方式)起作用,但是数据集之间的列数不匹配。 I need to have in every data set the maximum number of colums for every node in the whole file. 我需要在每个数据集中为整个文件中的每个节点设置最大列数。

In the end one should be able to open the csv-file with excel. 最后,应该可以使用excel打开csv文件。 Therefore each column need to contain the same data... is there any way to do this? 因此,每一列都需要包含相同的数据...有什么办法可以做到这一点?

Thanks! 谢谢!

Edit: In this case the expected csv-output would look like this: 编辑:在这种情况下,预期的csv输出将如下所示:

Blabla;2012;12;2013;1234;abc
Blubblub;;;;;def

... and NOT: ... 并不是:

Blabla;2012;12;2013;1234;abc
Blubblub;def

Given your expected CSV output (actually SSV, semicolon-separated-values, it seems), I don't think you want an inner for-each at all. 给定您期望的CSV输出(似乎实际上是SSV,用分​​号分隔的值),我认为您根本不需要内部for-each I would do it more along the lines of: 我会按照以下方式做更多:

<xsl:for-each select="Test">
    <xsl:value-of select="Field1"/>
    <xsl:text>;</xsl:text>
    <xsl:value-of select="Values[1]/Year"/>
    <xsl:text>;</xsl:text>
    <xsl:value-of select="Values[1]/Value"/>
    <xsl:text>;</xsl:text>
    <xsl:value-of select="Values[2]/Year"/>
    <xsl:text>;</xsl:text>
    <xsl:value-of select="Values[2]/Value"/>
    <xsl:text>;</xsl:text>

    <!-- etc, up to the final... -->

    <xsl:value-of select="Field2"/>
</xsl:for-each>

This gives you a fixed number of fields, with empty values where no node exists. 这为您提供了固定数量的字段,其中空值不存在任何节点。

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

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