2016-12-13 5 views
0

複数の要素と兄弟のテキストノードを1つの要素として結合するための提案が必要です。下記のサンプルのxref要素を参照してください。複数の要素を1つの要素として結合する

Input: <section><p>These pages are all about XSLT, an XML-based language <xref ref-type="bibr" rid="r1">1</xref><xref ref-type="bibr" rid="r2"/>--<xref ref-type="bibr" rid="r3">3</xref> for translating one set of XML into another set of XML, <xref ref-type="bibr" rid="r3">3</xref>, <xref ref-type="bibr" rid="r5">5</xref><xref ref-type="bibr" rid="r6"/>--<xref ref-type="bibr" rid="r7">7</xref> or into HTML. Of course, there are all sorts of other pages <xref ref-type="bibr" rid="r1">7</xref>, <xref ref-type="bibr" rid="r3">8</xref> around that cover XSLT. <xref ref-type="bibr" rid="r12">12</xref>, <xref ref-type="bibr" rid="r15">15</xref><xref ref-type="bibr" rid="r16"/><xref ref-type="bibr" rid="r17"/><xref ref-type="bibr" rid="r18"/><xref ref-type="bibr" rid="r19"/>--<xref ref-type="bibr" rid="r20">20</xref></p></section> 

Output: <section><p>These pages are all about XSLT, an XML-based language <xref ref-type="bibr" rid="r1 r2 r3">1--3</xref> for translating one set of XML into another set of XML, <xref ref-type="bibr" rid="r3 r5 r6 r7">3, 5--7</xref> or into HTML. Of course, there are all sorts of other pages <xref ref-type="bibr" rid="r7 r8">7, 8</xref> around that cover XSLT. <xref ref-type="bibr" rid="r12 r15 r16 r17 r18 r19 r20">12, 15--20</xref></p></section> 

マージは文字(、)スペースをカンマまたは()スペースまたは場合にのみ起こるはず( - )2つのハイフンまたは空外部参照要素(<xref ref-type="bibr" rid="r2"/>)は外部参照要素わたってるしき表示されます。

E.g. 
Input content: <xref ref-type="bibr" rid="r3">3</xref>, <xref ref-type="bibr" rid="r5">5</xref><xref ref-type="bibr" rid="r6"/>--<xref ref-type="bibr" rid="r7">7</xref> 

Expected ouput: <xref ref-type="bibr" rid="r1 r2 r3">1--3</xref> 

おかげで、よろしく バラXSLT 2.0を使用して

+0

のようなアプローチを使用することができますあなたはマージしたいときとそうでないときにマージしたい。 'xref'要素のいくつかは隣接していますが、いくつかはテキストノードで区切られています。マージしたいテキストはどれですか? '--'と'、 'のみです。または任意の句読点? –

答えて

1

、あなたはfor-each-group select="node()" group-adjacent="boolean(self::xref | self::text()[matches(., $pattern)]を使用して隣接ノードを見つけることができますので、あなたは、少なくともあなたの質問を編集したときにルールを説明

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

    <xsl:template match="*[xref]"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:for-each-group select="node()" 
       group-adjacent="boolean(self::xref | self::text()[matches(., '^[\s\p{P}]+$')])"> 
       <xsl:choose> 
        <xsl:when test="current-grouping-key()"> 
         <xsl:copy> 
          <xsl:copy-of select="@* except @rid"/> 
          <xsl:attribute name="rid" select="current-group()/@rid"/> 
          <xsl:value-of select="current-group()"/> 
         </xsl:copy> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:apply-templates select="current-group()"/> 
        </xsl:otherwise> 
       </xsl:choose> 
      </xsl:for-each-group> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
+0

ありがとう、これは私が正確にしたいものです。しかし、xref要素の内部には不必要なスペースがいくつか表示されます。 (3-5の代わりに1-3、3-5,5-7の代わりに3-5-5)。 – Bala

+0

不要な空白については、「 = current-group() "に変更してください。separator =" "/ > '。 –

関連する問題