2016-12-13 2 views
0

여러 요소와 형제 텍스트 노드를 단일 요소로 병합하려면 제안이 필요합니다. 아래에 언급 된 샘플에서 xref 요소를 참조하십시오.여러 요소를 단일 요소로 병합

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> 

병합 현상이 발생하는 경우에만 문자 (,) 공간 또는() 공백이나와 쉼표 (-) 두 개의 하이픈 또는 빈 외부 참조 요소 (<xref ref-type="bibr" rid="r2"/>가) 외부 참조 요소 inbetween 나타납니다.

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> 

감사합니다 감사합니다 발라

+0

같은 방법을 사용할 수 있습니다 for-each-group select="node()" group-adjacent="boolean(self::xref | self::text()[matches(., $pattern)]를 사용하여 인접 노드를 찾을 때 규칙을 설명 할 수 병합하고 싶을 때와 병합하고 싶을 때. 'xref' 요소 중 일부는 인접하지만 일부는 텍스트 노드로 구분됩니다. 병합하고자하는 텍스트는 어느 것이고, 단지'--'와','입니까? 아니면 구두점이 있습니까? –

답변

1

XSLT 2.0을 사용하여, 당신은 적어도 당신의 질문을 수정 그래서 당신은

<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

대단히 감사합니다. 정확히 내가 원하는 것입니다. 그러나 일부 불필요한 공간은 외부 참조 요소 내부에 나타납니다. (1 - 3 대신 1 - 3 및 3, 5 - 7 대신 3, 5 - 7). – Bala

+0

불필요한 공백에 관해서는, '을 '. –