2016-03-19 17 views
0

";"を使用してカスタムXMLをCSVに変換したい。値区切り記号として使用します。カスタムXMLをCSVに変換するXSLT

<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="root/operators/item" /> 
    </xsl:template> 

    <xsl:template match="item"> 
    <xsl:apply-templates /> 
    <!--<xsl:if test="following-sibling::*">--> 
     <xsl:value-of select="concat($delim, $break)" /> 
    <!--</xsl:if>--> 
    </xsl:template> 

    <xsl:template match="*"> 
    <xsl:value-of select="normalize-space()" /> 
    <xsl:if test="following-sibling::*"> 
     <xsl:value-of select="$delim" /> 
    </xsl:if> 
    </xsl:template> 

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

</xsl:stylesheet> 
:次のXSLT変換を使用する(特にコードが彼の答え ...a version with configurable parameters that you can set programmaticallyに@Tomalakで書いて使用して、stackoverflowのスレッド XML to CSV Using XSLTを使用して)私がこれまで管理してきた

<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <operators> 
     <item> 
      <lfbis>1234567</lfbis> 
      <name>Stefan Maier</name> 
      <company /> 
      <street>Testweg 7</street> 
      <citycode>95131</citycode> 
      <city>Hof</city> 
      <controlbody>BIKO</controlbody> 
      <productdata> 
       <item>Rinder</item> 
       <item>9</item> 
      </productdata> 
     </item> 
     <item> 
      <lfbis>5671234</lfbis> 
      <name>Antom Mueller</name> 
     <company>Berghof</company> 
      <street>Testweg 8</street> 
      <citycode>95111</citycode> 
      <city>Bamberg</city> 
      <controlbody>BIKO</controlbody> 
      <productdata> 
       <item>Rinder</item> 
       <item>9</item> 
      </productdata> 
     </item> 
    </operators> 
</root> 

:ここに私のXMLです私が達成しようとしていることということで、今

1234567;Stefan Maier;;Testweg 7;95131;Hof;BIKO;Rinder 9; 
5671234;Antom Mueller;Berghof;Testweg 8;95111;Bamberg;BIKO;Rinder 9; 

は...と私はxsltprocのを使用して次の結果を取得しています品目の製品データ要素は、csv結果の値としても扱われます。だから私は ";"代わりに、このような私のCSVなど、 9 Rinder値の間とは次のようになります(スペース) "" で:

ここ
1234567;Stefan Maier;;Testweg 7;95131;Hof;BIKO;Rinder;9; 
5671234;Antom Mueller;Berghof;Testweg 8;95111;Bamberg;BIKO;Rinder;9; 
+2

他の人のコードを使用する場合は、書き込んだと言っているのではなく、少なくともクレジットカードにクレジットを入れてください。ここからあなたのXSLTコードはhttp://stackoverflow.com/a/365372/18771にあります。 – Tomalak

+0

はい、申し訳ありませんが、私はもちろん、xlstスタイルシートを書くためにstackoverflowスレッドを使用しました。私はそのスレッドの人に、特に私の質問で彼を言及していないためのスタイルシートの作者には謝罪します。私は将来の投稿にもっと注意する。 –

答えて

1

あなたが持っているとしてTomalakのコード

<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="root/operators/item" /> 
    </xsl:template> 

    <xsl:template match="root/operators/item"> 
    <xsl:apply-templates select=".//*[not(*)]"/> 
    <xsl:value-of select="$break" /> 
    </xsl:template> 

    <xsl:template match="*"> 
    <xsl:if test="position() > 1"> 
     <xsl:value-of select="$delim"/> 
    </xsl:if> 
    <xsl:value-of select="normalize-space()" /> 
    </xsl:template> 

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

</xsl:stylesheet> 

の適応であります2種類の要素のうち、私はかなり明白なマッチパターンが必要でした。

+0

ありがとうマーティン、私はあなたの@Tomalakのコードのあなたの適応を使用し、xml getは私が望むように変形しました。各行の最後の値の後ろにセミコロンを置くために、テンプレートの_root/operators/item_要素にマッチする 'select =" concat($ delim、$ break) "'を追加しました。 –

2

1つの簡単な方法は、子要素を持たない要素と一致する

<xsl:template match="*[not(*)]"> 

<xsl:template match="*"> 

を変更することです。 apply-templatesがproductData要素にヒットすると、一致するテンプレートルールが存在しないので、テンプレートに子供を適用するデフォルトが起動します。

ただし、これには区切り記号を処理するための戦略の変更が必要です。あなたの場合は、最後のものを含むすべての項目の後にセミコロンを入れたいので、かなり簡単です。各項目を出力した後に無条件にセミコロンを追加し、行の最後にセミコロンを追加しないでください。

+0

マイケルが正しい方向に向いてくれてありがとう! –

関連する問題