2010-12-07 19 views
1

<amendment/>要素の場合は、<sectionid/>,<amendmenttext/>および<sponsor/>という要素がある場合はそれを取得する必要があります。ここXSLTで前の兄弟のグループを取得する

は一例であり:

から:

<root> 
     <amendment> 
      <id>1</id> 
      <text>some text</text> 
     </amendment> 
     <sectionid>A</sectionid> 
     <sponsor>jon</sponsor> 
     <sponsor>peter</sponsor> 
     <amendment> 
      <id>8</id> 
      <text>some text</text> 
     </amendment> 
     <sectionid>B</sectionid> 
     <sponsor>matt</sponsor> 
     <sponsor>ben</sponsor> 
     <amendmenttext>some intro text</amendmenttext> 
     <amendment> 
      <id>5</id> 
      <text>some text</text> 
     </amendment> 
     <amendment> 
      <id>4</id> 
      <text>some text</text> 
     </amendment> 
     <sponsor>max</sponsor> 
     <amendment> 
      <id>6</id> 
      <text>some text</text> 
     </amendment> 
     <amendment> 
      <id>7</id> 
      <text>some text</text> 
     </amendment> 

</root> 

に:

<root> 
     <amendment>     
      <id>1</id> 
      <text>some text</text> 
     </amendment>   
     <amendment> 
      <sectionid>A</sectionid> 
      <sponsor>jon</sponsor> 
      <sponsor>peter</sponsor> 
      <id>8</id> 
      <text>some text</text> 
     </amendment> 
     <amendment> 
      <sectionid>B</sectionid> 
      <sponsor>matt</sponsor> 
      <sponsor>ben</sponsor> 
      <amendmenttext>some intro</amendmenttext> 
      <id>5</id> 
      <text>some text</text> 
     </amendment> 
     <amendment> 
      <sectionid>B</sectionid> 
      <sponsor>matt</sponsor> 
      <sponsor>ben</sponsor> 
      <id>4</id> 
      <text>some text</text> 
     </amendment> 
     <amendment> 
      <sectionid>B</sectionid> 
      <sponsor>max</sponsor> 
      <id>6</id> 
      <text>some text</text> 
     </amendment> 
     <amendment> 
      <sectionid>B</sectionid> 
      <sponsor>max</sponsor> 
      <id>7</id> 
      <text>some text</text> 
     </amendment> 

</root> 

注1:<sectionid/>要素は次<sectionid/>

前に、すべての<amendments/>に適用します注2:<sponsor/>要素は、次の<sponsor/>リストの前のすべての<amendments/>に適用されます。

注3://amendment/idの値は連続していません。

注4:<amendment/>は、前の兄弟として<sponsor/>または<sectionid/>を持つことができませんでした。

注5:<amendmenttext>のみに適用され、以下、この変換は、XSLT 1.0で行うことができますどのように<amendment/>

+0

前の質問からの変更http://stackoverflow.com/questions/4096481/refactor-xml-with-xslはマイナーです。私はそれが新しい質問に値するとは思わない。 –

+0

沖。解決策が似ていることを知らない。元の回答者が回答した後に質問の文脈を変更することは公正ではないと思った。しかし、要点。 –

答えて

2

このスタイルシート(同前よりも答えが、ただ一つのルール):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="node()|@*"> 
     <xsl:param name="pSectionId" select="/.."/> 
     <xsl:param name="pSponsors" select="/.."/> 
     <xsl:variable name="vSectionid" select="self::sectionid"/> 
     <xsl:variable name="vSponsor" select="self::sponsor"/> 
     <xsl:variable name="vAmendment" select="self::amendment"/> 
     <xsl:if test="not($vSectionid|$vSponsor)"> 
      <xsl:copy> 
       <xsl:apply-templates select="@*"/> 
       <xsl:copy-of select="$pSectionId[$vAmendment]"/> 
       <xsl:copy-of select="$pSponsors[$vAmendment]"/> 
       <xsl:apply-templates select="node()[1]"/> 
      </xsl:copy> 
     </xsl:if> 
     <xsl:apply-templates select="following-sibling::node()[1]"> 
      <xsl:with-param name="pSectionId" 
          select="$pSectionId[not($vSectionid)]|$vSectionid"/> 
      <xsl:with-param name="pSponsors" 
          select="$pSponsors[not($vSponsor) or 
               current() 
                /preceding-sibling::node()[1] 
                /self::sponsor] | 
            $vSponsor"/> 
     </xsl:apply-templates> 
    </xsl:template> 
</xsl:stylesheet> 

出力:

<root> 
    <amendment> 
     <id>1</id> 
     <text>some text</text> 
    </amendment> 
    <amendment> 
     <sectionid>A</sectionid> 
     <sponsor>jon</sponsor> 
     <sponsor>peter</sponsor> 
     <id>8</id> 
     <text>some text</text> 
    </amendment> 
    <amendment> 
     <sectionid>B</sectionid> 
     <sponsor>matt</sponsor> 
     <sponsor>ben</sponsor> 
     <id>5</id> 
     <text>some text</text> 
    </amendment> 
    <amendment> 
     <sectionid>B</sectionid> 
     <sponsor>matt</sponsor> 
     <sponsor>ben</sponsor> 
     <id>4</id> 
     <text>some text</text> 
    </amendment> 
    <amendment> 
     <sectionid>B</sectionid> 
     <sponsor>max</sponsor> 
     <id>6</id> 
     <text>some text</text> 
    </amendment> 
    <amendment> 
     <sectionid>B</sectionid> 
     <sponsor>max</sponsor> 
     <id>7</id> 
     <text>some text</text> 
    </amendment> 
</root> 

:前の回答からdiference式を設定し、デフォルト空のノードであります必要とされるそれらの規則のパラメータのために。

+0

@Alejandro、サンプルXMLにを追加しました。 –

+0

@Benjamin Ortuzar:この時点で、これに応じて、以前の回答を変更する方法を知っておく必要があります。空のノードセット '/ ..'をdefaut値として追加する 'pAmendmentText'パラメータを追加し、変数' vAmendmentText'を 'self :: amendmenttest'として追加し、この変数を' xsl:if/@ 'に追加します。 '$ pAmendmentText [$ vAmendmentText'($ vAmendment)| $ vAmendmentText']で' pAmendmentText'パラメタを渡して、 '$ pAmendmentText [$ vAmendment]'で 'xsl:copy-of'命令を追加します。 –

関連する問題