LoginSignup
Amn378
@Amn378

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

XSL でCSV出力時 idを指定して項目を出力したい

解決したいこと

Redmineから出力したxmlをcsvデータに落とすためのxslを作成中です。
custom_field以外の項目はすでに成功しています。custom_fieldのidをベタ書きしてcsv出力項目として設定したいのですが、うまくいきません。
for eachで出力する事例は検索して出てくるのですが、idが事前にわかっているので、id指定で項目を出力する書き方を教えてください。
よろしくお願いいたします。

###昨日XSLを触り始めたばかりの初学者ですので他にも間違いがあるかもしれません。

Sample XML(抜粋)

<?xml version="1.0" encoding="UTF-8"?>
<issues total_count="1" offset="0" limit="100" type="array">
<issue>
<start_date>2024-04-09</start_date>
<due_date>2024-04-15</due_date>
<custom_fields type="array">
<custom_field id="29" name="業務担当者">
<value>111</value>
</custom_field>
<custom_field id="14" name="プロセス">
<value>SSSS</value>
</custom_field>
<custom_field id="50" name="管理担当者">
<value>333</value>
</custom_field>
</custom_fields>
</issues>

xsl

<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="Shift_JIS" indent="no" />

  <xsl:template match="/">
    <xsl:apply-templates select="issues" />
  </xsl:template>
  
  
<xsl:template match="issue">
<xsl:value-of select="start_date"/>,<xsl:value-of select="due_date"/>,<xsl:value-of select="custom_field/id('29')"/>
<xsl:text>&#10;</xsl:text>
</xsl:template>
  
</xsl:stylesheet>

自分で試したこと

様々な記載方法を試してみた。

0

1Answer

特定の値の id 属性を持つ要素を選択する XPath は [@id='...'] と書きます。以下のようにすれば custom_field を取り出すことができます。(取り出した項目の前後に不要な改行が入らないように normalize-space() をかけています)

<xsl:value-of select="normalize-space(custom_fields/custom_field[@id='29'])"/>
0Like

Comments

  1. @Amn378

    Questioner

    ありがとうございます!
    思っていたことが実現できました。
    昨日仕事終了後も気になって仕方なかったので、本当に助かりました!
    ありがとうございました。

Your answer might help someone💌