2016-08-05 2 views
3

XMLファイルをCSVファイルに変換したいと思います。エクスポートしたいフィールドは<issues>です。各問題には、標準フィールドとカスタムフィールドがあります。可能なカスタムフィールドは<issue-custom-fields>に定義されています。 <issue>にすべてのカスタムフィールドが設定されているわけではありません。 CSVのエクスポートのために、私は空のフィールド(",,")をそれぞれの不足しているエントリに追加する必要があります。どうすればいい?xsltテンプレート内でキーを作成

次のxsltファイルは、問題のすべてのフィールドとすべてのカスタムフィールドをループします。 「フィールド値?」の代わりに現在の項目のそれぞれのフィールド値を検索したいと思います(存在する場合は空白、存在しない場合は空白)。

XSLTのバージョンは1.0にする必要があります。以下のデータに作用する

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output method="text" /> 

    <xsl:template match="/"> 
     <xsl:apply-templates select="xml_data/issues"/> 
    </xsl:template> 

    <xsl:template match="issue"> 
     <xsl:for-each select="*"> 
      <xsl:choose> 
       <xsl:when test="name(.) = 'custom-values'"> 
        <xsl:for-each select="/xml_data/issue-custom-fields/issue-custom-field">         
         <xsl:variable name="f" select="id" /> 

         <xsl:text>field value?</xsl:text> 

         <xsl:if test="position() != last()"> 
          <xsl:text>,</xsl:text> 
         </xsl:if> 
        </xsl:for-each> 
       </xsl:when> 

       <xsl:otherwise> 
        <xsl:value-of select="."/>   
        <xsl:if test="position() != last()"> 
         <xsl:text>,</xsl:text> 
        </xsl:if> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:for-each>   
    </xsl:template> 
</xsl:transform> 

<?xml version="1.0" encoding="UTF-8"?> 
    <xml_data> 
     <projects type="array"> 
      <project> 
       <id type="integer">10</id> 
       <name>Helpdesk</name> 
       <type>open</type> 
      </project> 
      <project> 
       <id type="integer">20</id> 
       <name>Development</name> 
       <type>closed</type> 
      </project> 
     </projects> 

     <issue-custom-fields> 
      <issue-custom-field> 
       <id>1000</id> 
       <name>Delay</name> 
      </issue-custom-field> 
      <issue-custom-field> 
       <id>1001</id> 
       <name>Means of Delivery</name> 
      </issue-custom-field> 
      <issue-custom-field> 
       <id>1002</id> 
       <name>Shipping Date</name> 
      </issue-custom-field> 
     </issue-custom-fields> 

     <issues type="array"> 
      <issue> 
       <id type="integer">100</id> 
       <project-id type="integer">10</project-id> 
       <subject>first helpdesk issue</subject> 
       <description>a small problem</description> 
       <custom-values> 
        <custom-value> 
         <custom-field-id>1000</custom-field-id> 
         <value>15</value> 
        </custom-value> 
        <custom-value> 
         <custom-field-id>1002</custom-field-id> 
         <value>2016-08-01</value> 
        </custom-value> 
       </custom-values> 
      </issue> 
      <issue> 
       <id type="integer">101</id> 
       <project-id type="integer">10</project-id> 
       <subject>second helpdesk issue</subject> 
       <description>a medium problem</description> 
       <custom-values> 
        <custom-value> 
         <custom-field-id>1000</custom-field-id> 
         <value>10</value> 
        </custom-value> 
        <custom-value> 
         <custom-field-id>1001</custom-field-id> 
         <value>FTP</value> 
        </custom-value> 
       </custom-values> 
      </issue> 
      <issue> 
       <id type="integer">102</id> 
       <project-id type="integer">10</project-id> 
       <subject>third helpdesk issue</subject> 
       <description>a huge security problem</description> 
       <custom-values> 
        <custom-value> 
         <custom-field-id>1001</custom-field-id> 
         <value>SSH</value> 
        </custom-value> 
       </custom-values> 
      </issue> 
      <issue> 
       <id type="integer">103</id> 
       <project-id type="integer">20</project-id> 
       <subject>first "development" issue</subject> 
       <description>just some "strange" software</description> 
       <custom-values> 
       </custom-values> 
      </issue> 
     </issues> 
    </xml_data> 

あなたの助けをありがとうございました。

+0

XMLタグを書き込むときテキストではバッククォート( ')で囲んでコードとしてフォーマットし、HTMLサニタイザでは削除しないでください。私はあなたのためにそれを固定した。 –

+0

サンプル入力から生成する出力を表示してください。これがXSLT 1か2であるかどうかも教えてください。 –

答えて

1

私が正しく理解していれば、あなたがしたい:

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:key name="custom-value" match="custom-value" use="concat(custom-field-id, '|', ancestor::issue/id)" /> 

<xsl:template match="/xml_data"> 
    <xsl:variable name="custom-fields" select="issue-custom-fields/issue-custom-field" /> 
    <!-- header --> 
    <!-- standard fields --> 
    <xsl:for-each select="issues/issue[1]/*[not(self::custom-values)]"> 
     <xsl:value-of select="name()"/>   
     <xsl:text>,</xsl:text> 
    </xsl:for-each> 
    <!-- custom fields --> 
    <xsl:for-each select="$custom-fields"> 
     <xsl:value-of select="name"/>   
     <xsl:if test="position() != last()"> 
      <xsl:text>,</xsl:text> 
     </xsl:if> 
    </xsl:for-each> 
    <xsl:text>&#10;</xsl:text> 
    <!-- data --> 
    <xsl:for-each select="issues/issue"> 
     <xsl:variable name="issue-id" select="id" /> 
     <!-- standard fields --> 
     <xsl:for-each select="*[not(self::custom-values)]"> 
      <xsl:value-of select="."/>   
      <xsl:text>,</xsl:text> 
     </xsl:for-each> 
     <!-- custom fields --> 
     <xsl:for-each select="$custom-fields"> 
      <xsl:value-of select="key('custom-value', concat(id, '|', $issue-id))/value"/>   
      <xsl:if test="position() != last()"> 
       <xsl:text>,</xsl:text> 
      </xsl:if> 
     </xsl:for-each> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 

、結果は以下のようになります。

id,project-id,subject,description,Delay,Means of Delivery,Shipping Date 
100,10,first helpdesk issue,a small problem,15,,2016-08-01 
101,10,second helpdesk issue,a medium problem,10,FTP, 
102,10,third helpdesk issue,a huge security problem,,SSH, 
103,20,first "development" issue,just some "strange" software,,, 
+0

ありがとうございました。コンカットと祖先でキーを作成するというアイデアは素晴らしいです。 – divingTobi

+0

小さな調整:concatステートメントにダッシュ( " - ")を追加することにより、 カスタムフィールドID = 101および/ id = 10の対カスタムフィールドID = 10と発行/ id = 110:concat(カスタムフィールドID、 ' - '、祖先::問題/ ID) – divingTobi

+0

@divingTobiあなたは絶対に正しいです。私は常に連結された値の間に '|'を置いています。私はなぜこのようなことを怠ったのか分かりません。 –

関連する問題