3

この構造を変換する必要があります

<A>
<B>value1</B>
</A>
<A>
<B>value2</B>
</A>

の中へ

<A>
<B>value1<B>
<B>value2<B>
</A>

XSLT-1.0を使用する最良の解決策は何ですか?ありがとうございました!

PS:私はこのコードを試しました:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>   
<xsl:key name="group_a" match="//A" use="B"/> 
<xsl:template match="/Test"> <a-node> <xsl:for-each select="//A"> <b-node> 
<xsl:value-of select="//A/B"/> </b-node> </xsl:for-each> </a-node> 
</xsl:template> 
</xsl:stylesheet> 

ただし、最初の値のみを返します。

<?xml version="1.0" encoding="utf-8"?> <a-node mlns:fo="http://www.w3.org/1999/XSL/Format"> <b-node>value1</b-node> <b-node>value1</b-node> </a-node> 

しかし、私は必要です:

<?xml version="1.0" encoding="utf-8"?> <a-node xmlns:fo="http://www.w3.org/1999/XSL/Format"> <b-node>value1</b-node> <b-node>value2</b-node> </a-node>
4

3 に答える 3

2

このスタイルシート...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
    <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
    <xsl:template match="/">
       <A>
        <xsl:apply-templates select="*/A/B"/>
       </A>
    </xsl:template>

    <xsl:template match="B">
      <B><xsl:value-of select="."/></B>
    </xsl:template>
</xsl:stylesheet>

...変形します...

<root>
<A>
<B>value1</B>
</A>
<A>
<B>value2</B>
</A>
</root>

...これに...

 <A><B>value1</B><B>value2</B></A>
于 2012-05-24T14:10:53.653 に答える
1

1つのノードの下ですべての子を折りたたむ必要があるように思われるため、「A」のforeachは必要なく、「B」の子に直接移動できます。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
    <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
    <xsl:template match="/">
        <A>
            <xsl:for-each select="//A/B">
                <B>
                    <xsl:value-of select="./text()"/>
                </B>
            </xsl:for-each>
        </A>
    </xsl:template>
</xsl:stylesheet>

編集@Seanのコメントによると、//実際には決して使用されるべきではないことに注意してください。//を実際のルート要素からのパスに置き換えます。

于 2012-05-24T12:33:47.497 に答える
0

この変換では、最も基本的なXSLTデザインパターンの1つ、つまりID変換をオーバーライドします。したがって、以下を記述、理解、維持、および拡張する方が簡単です

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

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

 <xsl:template match="A[1]">
  <A>
    <xsl:apply-templates select="node()|following-sibling::A/node()"/>
  </A>
 </xsl:template>
 <xsl:template match="A"/>
</xsl:stylesheet>

この変換が次のXMLドキュメントに適用される場合(提供されたXMLフラグメントを単一の最上位要素にラップすることによって取得されます-これを整形式のXMLドキュメントにします):

<t>
    <A>
        <B>value1</B>
    </A>
    <A>
        <B>value2</B>
    </A>
</t>

必要な正しい結果が生成されます。

<t>
   <A>
      <B>value1</B>
      <B>value2</B>
   </A>
</t>
于 2012-05-25T02:30:24.023 に答える