2016-03-21 17 views
1

私は数時間XSLTに苦労していて、私はStackOverflowに助けを求めました。私は、これが驚くほど難しいことではありません知っているが、私はそれを得ることはありません... XSLTをこのいくつかのXML要素をXSLTでグループ化する

<?xml version="1.0" encoding="UTF-8"?> 
<div> 
<p>Some regular text</p> 

<group> 
    <p>The first part</p> 
    <p>and then some text</p> 
    <p>and some more</p> 
</group> 

<p>Some regular text</p> 
<p>Some regular text</p> 
<p>Some regular text</p> 

<group> 
    <p>Here some new text</p> 
    <p>and some more</p> 
    <p>and the end of this section</p> 
</group> 

<p>Some regular text</p> 
</div> 

で終わるしたい

<?xml version="1.0" encoding="UTF-8"?> 
<div> 
<p class="text">Some regular text</p> 
<p class="first">The first part</p> 
<p class="next">and then some text</p> 
<p class="next">and some more</p> 

<p class="regular">Some regular text</p> 
<p class="text1">Some regular text</p> 
<p class="text2">Some regular text</p> 
<p class="first">Here some new text</p> 
<p class="next">and some more</p> 
<p class="next">and the end of this section</p> 
<p class="text3">Some regular text</p> 
</div> 

:私はこのようないくつかの入力テキストを持っています私は今持っている:誰も私を助けることができる場合、私は本当にXSLTがどのように機能するかを理解する問題を抱えているので、私は、非常に感謝される:(まったく動作しない

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
exclude-result-prefixes="xs" 
version="2.0"> 
<xsl:template match="/"> 
    <xsl:apply-templates select="*" /> 
</xsl:template> 

<xsl:template match="*"> 
    <xsl:copy> 
     <xsl:for-each select="@*"> 
      <xsl:copy /> 
     </xsl:for-each> 
     <xsl:apply-templates /> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="div"> 
    <lg> 
     <xsl:apply-templates/> 
    </lg> 
</xsl:template> 


<xsl:template match="/div"> 
    <xsl:for-each-group select="//p[@class='first']/following-sibling::p[@class='next']" group-by="text()"> 
     <group> 
      <xsl:apply-templates/> 
     </group> 
    </xsl:for-each-group> 
</xsl:template> 
</xsl:stylesheet> 

...

敬具、 ホセ・

答えて

2

あなたは、すべての「次」のものに続いて、グループへの「最初」に設定class属性を持っているpしようとしています。あなたの与えられたXMLのために

、このXSLTは、トリックを行うことがあります。それは一つのグループ内のすべてのpの要素を置くよう

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
<xsl:output method="xml" indent="yes" /> 

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

    <xsl:template match="/div"> 
    <xsl:for-each-group select="p" group-adjacent="@class = 'first' or @class='next'"> 
     <xsl:choose> 
      <xsl:when test="@class = 'first' or @class='next'"> 
       <group> 
        <xsl:apply-templates select="current-group()" /> 
       </group> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:apply-templates select="current-group()" /> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:for-each-group> 
</xsl:template> 

<xsl:template match="@class" /> 
</xsl:stylesheet> 

しかし、これは、このケースでは動作しないでしょうし。

<div> 
<p class="first">The first part</p> 
<p class="next">and then some text</p> 
<p class="first">Here some new text</p> 
<p class="next">and some more</p> 
</div> 

これはないあなたが望むものであるならば、その答えは、このXSLTではなく

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
<xsl:output method="xml" indent="yes" /> 

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

    <xsl:template match="/div"> 
    <xsl:for-each-group select="p" group-starting-with="p[@class='first']"> 
     <xsl:for-each-group select="current-group()" group-adjacent="@class='first' or @class='next'"> 
     <xsl:choose> 
      <xsl:when test="@class='first' or @class='next'"> 
       <group> 
        <xsl:apply-templates select="current-group()" /> 
       </group> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:apply-templates select="current-group()" /> 
      </xsl:otherwise> 
     </xsl:choose> 
     </xsl:for-each-group> 
    </xsl:for-each-group> 
</xsl:template> 

<xsl:template match="@class" /> 
</xsl:stylesheet> 
+0

こんにちはティム、感謝をしてみてください!!私が理解している限り、あなたはこのpを否定的な方法で定義することを選択しています。pはクラスを持たないものです。私たちはそれを積極的に試すことができますか?その特定のクラスの要素を選択しますか?私はこのように驚きを入れたら、うまくいきませんでした。 –

+0

xml入力を編集してそれをより良くカバーしました。 –

+1

私は2つの可能なXSLTで答えを更新しました。 –

関連する問題