2011-07-06 24 views
3

XSLTを使用して同じ要素名を持つすべての子要素を持つ親要素を転送する方法があるのだろうかと思います。例えばXSLT/XSLを使用して同じ名前の子要素を持つXMLを解析する

、元のXMLファイルは、次のようにある場合:

<parent> 
    <child>1</child> 
    <child>2</child> 
    <child>3</child> 
</parent> 

そして、私が使用してXSLとそれを解析しよう:

このような何か欠けて
<xsl:for-each select="parent"> 
    <print><xsl:value-of select="child"></print> 

<print>1</print> 
<print>2</print> 
<print>3</print> 

しかし、私はこれを得る:

<print>1</print> 

のために、それぞれがよりこのフォーマットのために設計されているので:

<parent> 
    <child>1</child> 
<parent> 
</parent 
    <child>2</child> 
<parent> 
</parent 
    <child>3</child> 
</parent 

は、それが上記であるようにそれをフォーマットすることなく、所望の印刷結果を得るためにとにかくではなく、最初の方法はありますか?

おかげ

答えて

4

あなたの代わりに、子の親にxsl:for-eachをやっているからです。あなたは、あなたがこれにそれを変更した場合を探している結果を得るでしょう(現在のコンテキストを想定し/です):xsl:for-eachは通常必要ありません使用して

しかし
<xsl:for-each select="parent/child"> 
    <print><xsl:value-of select="."/></print> 
</xsl:for-each> 

...

<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="parent"> 
    <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="child"> 
     <print><xsl:apply-templates/></print> 
    </xsl:template> 

</xsl:stylesheet> 

:あなたはここでは例の完全なスタイルシートです

オーバーライドテンプレートがあなたのために仕事を扱う代わりに、単一のテンプレート/コンテキスト(のような/)からすべての子に取得しようとさせてくださいこのスタイルシートの出力は次のようになります。

<print>1</print> 
<print>2</print> 
<print>3</print> 
関連する問題