- PR -

ある要素の値をある要素の属性に

1
投稿者投稿内容
hirotan
常連さん
会議室デビュー日: 2004/07/20
投稿数: 41
投稿日時: 2004-07-21 08:13
ある階層にある要素の値を任意の要素の属性に加えたいのですが、どうしたらいいでしょうか。
初心者チックな質問で申し訳ありません。
ここではまってしまって苦しんでおります。
よろしくお願いします。

例:<add1>,<add2>要素の値を<c>の属性として追加したい。
<root>
 <a>
  <b>
   <c att1="abc">
  </b>
 </a>
 <x>
  <y>
   <add1>追加1</add1>
   <add2>追加2</add2>
  </y>
 </x>
</root>

MMX
ぬし
会議室デビュー日: 2001/10/26
投稿数: 861
投稿日時: 2004-07-21 10:32
   <c att1="abc"/>
ちゃんと XML エディタとか通してから、貼ったほうがよい。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<!-- 固定の決め打ち カレントを頭に位置づけて、ランダムアクセス -->
<c>
<xsl:attribute name="att1"><xsl:value-of select="a/b/c/@att1"/></xsl:attribute>
<xsl:attribute name="add1"><xsl:value-of select="x/y/add1"/></xsl:attribute>
<xsl:attribute name="add2"><xsl:value-of select="x/y/add2"/></xsl:attribute>
</c>
</xsl:template>
</xsl:stylesheet>
hirotan
常連さん
会議室デビュー日: 2004/07/20
投稿数: 41
投稿日時: 2004-07-21 15:20
MMXさん 回答ありがとうございます。
すみませんこの次はちゃんとエディタを通して貼る事にします。

ちょっとイメージと違っていて、XMLの構造をそのままにして、add1とadd2の値を<c>の属性に追加したいです。

ちょっとXSLを作ってみました。変数の有効範囲的にうまくできていないのはわかるのですが、やりたいことのニュアンスはこんな感じです。
よろしくお願いします。


<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />

<xsl:variable name="charatt" select="document('charatt.xml')" />

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

<xsl:template match="root">
<xsl:variable name="add1">
<xsl:value-of select="x/y/add1" />
</xsl:variable>
<xsl:variable name="add2">
<xsl:value-of select="x/y/add2" />
</xsl:variable>
</xsl:template>

<xsl:template match="c">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="add1"><xsl:copy-of select="$add1" /></xsl:attribute>
<xsl:attribute name="add2"><xsl:copy-of select="$add2" /></xsl:attribute>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
MMX
ぬし
会議室デビュー日: 2001/10/26
投稿数: 861
投稿日時: 2004-07-21 19:34
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- ここでは不要
<xsl:variable name="charatt" select="document('charatt.xml')" />
-->
<xsl:template match="* | @* | text()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="c"><!-- マッチの優先度 -->
<xsl:copy>
<xsl:copy-of select="@*"/>
<!-- 絶対パスで決め打ち 、相対でもできる -->
<xsl:attribute name="add1"><xsl:value-of select="/root/x/y/add1"/></xsl:attribute>
<xsl:attribute name="add2"><xsl:value-of select="/root/x/y/add2"/></xsl:attribute>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
=======================
<xsl:template match="c"><!-- マッチの優先度 -->
<c add1="{/root/x/y/add1}" add2="{/root/x/y/add2}">
<xsl:copy-of select="@*"/>
</c>
</xsl:template>
アトリビュート値テンプレートの場合。属性には順序はない

[ メッセージ編集済み 編集者: MMX 編集日時 2004-07-21 19:54 ]
hirotan
常連さん
会議室デビュー日: 2004/07/20
投稿数: 41
投稿日時: 2004-07-22 11:04
MMXさん 回答ありがとうございます。

XPATHで絶対パスなるものがあることをすっかり忘れていました!!
すみません。勉強不足でした。

これで解決できそうです。
どうもありがとうございました。
1

スキルアップ/キャリアアップ(JOB@IT)