How to select all text nodes between two elements using XSL?

I want to get generate-id(.)all text nodes after node <m/>and before node </n>. I am looking for some general XSL and am not tightly coupled with the sample input template mentioned below. For any input pattern, I want to get the identifiers of all text nodes between node <m/>and <n/>.

Input example for a better understanding:

<a>
  <b> 
    <c>
      This is first text node
    </c> 
  </b> 
  <d> 
    <e>
      This is my second text node
    </e> 
    <f> 
      This is my <m/>third text node 
    </f> 
    <g>
      One more text node
    </g>
  <h>
    <i>
      This is my fourth text node
    </i>
  </h>
  <j> 
    This is my fifth <n/>text node 
  </j>
  <k> 
    <l>
      This is my sixth text node 
    </l>
  </k>     
</d> 
</a> 

Expected Result: Create an identifier for text nodes with the values ​​"third text node", "Another text node", "This is my fourth text node", "This is my fifth", which lies between the nodes <m/>and<n/>

Please give your ideas.

+3
1

:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vtextPostM" select="//text()[preceding::m]"/>
 <xsl:variable name="vtextPreN" select="//text()[following::n]"/>

 <xsl:variable name="vtextBN-MandN" select=
  "$vtextPostM[count(.|$vtextPreN) = count($vtextPreN)]"/>

 <xsl:variable name="vNL" select="'&#xA;'"/>
 <xsl:variable name="vQ">"</xsl:variable>

 <xsl:template match="/">
   <xsl:for-each select="$vtextBN-MandN">
    <xsl:value-of select=
     "concat($vNL, 'Id: ', $vQ, generate-id(), $vQ,
            'Text: ', $vQ, ., $vQ)
     "/>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

, XML-, :

Id: "IDAOZDLB"Text: "third text node
    "
Id: "IDAQZDLBIDAQZDLB"Text: "
      One more text node
    "
Id: "IDAUZDLBIDAUZDLB"Text: "
      This is my fourth text node
    "
Id: "IDAYZDLB"Text: "
    This is my fifth "

Kaysian :

$ns1[count(.|$ns2)=count($ns2)]

, node $ns1, node $ns2.

+4

Source: https://habr.com/ru/post/1742354/


All Articles