0

このようなものを考えると:

<source>
 <somestuff>
  <thead>
   <row>C1</row>
  </thead>
  <tbody>
  <row>C2</row>
  <row>C3</row>
  </tbody>
 </somestuff>
 <somestuff>
  <tbody>
   <row>C4</row>
   <row>C5</row>
  </tbody>
 </somestuff>
 <somestuff>
  <tbody>
   <row>C6</row>
   <row>C7</row>
  </tbody>
 </somestuff>
</source>

最初の子として thead 要素の内容を次の tbody 要素にコピーする必要があります。(任意の数の thead 要素が存在する可能性があります。) 結果:

<source>
 <somestuff>
  <tbody>
  <row>C1</row>
  <row>C2</row>
  <row>C3</row>
  </tbody>
 </somestuff>
 <somestuff>
  <tbody>
   <row>C4</row>
   <row>C5</row>
  </tbody>
 </somestuff>
 <somestuff>
  <tbody>
   <row>C6</row>
   <row>C7</row>
  </tbody>
 </somestuff>
</source>

私はこれを試しました

 <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()|@*" name="identity" >
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

<xsl:template match="tbody/*[1]">
 <xsl:copy-of select=
     "preceding-sibling::*[1]
                      [name()='thead']/row"/>
<xsl:call-template name="identity"/>
 </xsl:template>


<!-- deleting the Head node  -->
 <xsl:template match="//thead"/>
</xsl:stylesheet>

しかし失敗しました。ありがとうございました。ラルフ

4

1 に答える 1

1

これを試して:

<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()|@*" name="identity" >
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="tbody/*[1]">
  <xsl:apply-templates select="../preceding-sibling::thead[1]/node()" />
  <xsl:call-template name="identity" />
 </xsl:template>

 <!-- deleting the Head node  -->
 <xsl:template match="thead"/>
</xsl:stylesheet>

元のスタイルシートの主な問題は、選択しようとしているということです

preceding-sibling::*[1][name()='thead']

コンテキスト ノードが のであり、それ自体でtbodyはない場合。私のバージョンでは、 を探す前に にアクセスするための がtbody追加されています。..tbodypreceding-sibling::thead[1]

tbodyこれは、空またはthead次のないaがある場合をカバーしていないことに注意してくださいtbodytheadこれらのケースをキャッチするには、代わりにテンプレートを作成する方がよい場合があります

<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()|@*" name="identity" >
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <!-- ignore a tbody that immediately follows a thead -->
 <xsl:template match="tbody[preceding-sibling::*[1]/self::thead]"/>

 <xsl:template match="thead">
  <tbody>
   <xsl:apply-templates select="@*|node()" />
   <xsl:apply-templates select="following-sibling::*[1]/self::tbody/node()" />
  </tbody>
 </xsl:template>
</xsl:stylesheet>

これにより、元の XML で同じ結果が得られますが、次のようなものにも対処できます。

 <somestuff>
  <thead>
   <row>C9</row>
  </thead>
 </somestuff>

また

 <somestuff>
  <thead>
   <row>C9</row>
  </thead>
  <tbody>
  </tbody>
 </somestuff>
于 2013-02-12T16:34:44.447 に答える