2012-03-16 20 views
0

XSLTを使用してXMLからXMLへの変換を行っています。要素から部分値を抽出し、別の要素に属性として割り当てる方法

私は要素からaprtial値を抽出して、それを新しい要素に属性として割り当てる必要があります。

ソースXML:

 <content> 
     <component> 
     <aaa>HI 
      <strong>[a_b_c]</strong> 
       : More Information Needed 
      <strong>[d_e_f]</strong>XXX 
     </aaa> 
    </component> 
    <content> 

ターゲットXML:

<ddd>hi<dv name='a_b_c'/>: More Information Needed <dv name='d_e_f'/> XXX 

    </ddd> 

いずれかのXSLTを通してそれを行う方法を提案することができます。

ありがとうございます。

答えて

0

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="aaa"> 
    <ddd> 
     <xsl:value-of select="substring-before(.,'[')"/> 
     <dv name="{substring-before(substring-after(.,'['),']')}"/> 
    </ddd> 
    </xsl:template> 

</xsl:stylesheet> 

又は

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="aaa"> 
    <ddd> 
     <xsl:value-of select="tokenize(.,'\[')[1]"/> 
     <dv name="{tokenize(tokenize(.,'\[')[2],'\]')[1]}"/> 
    </ddd> 
    </xsl:template> 

</xsl:stylesheet> 

EDIT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="aaa"> 
    <ddd> 
     <xsl:apply-templates select="node()|@*"/> 
    </ddd> 
    </xsl:template> 

    <xsl:template match="content|component"> 
    <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="strong"> 
    <dv name="{normalize-space(.)}"/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

ありがとうございました。質問を編集しましたが、再帰的に行う方法のように答えを変更できますか? – Patan

+0

@muzimil - 再帰的に何を意味するのか分かりません。あなたの新しい入出力の例では、文字列の操作はまったく必要ありません。 'content'、' component'、 'strong'要素を削除し、' aaa'要素の名前を変更するだけです。 –

+0

コンテンツの一部を削除していません。[a_b_c]などです。再帰的に言えば、複数回実行する必要がある(複数のものを削除する必要がある場合)ことを意味します。 – Patan

関連する問題