XSLT - select the following items before a specific tag

I have this xml file:

<text> <w>John</w> <x>Smith</x> <z>Andy</z> <w>Goo</w> <w>Joseph</w> <y>Lee</y> <x>Kenny</x> <z>Maria</z> <y>Zoe</y> <z>Martin</z> </text> 

Now I want to select elements between 1st <z> and 2nd <z>

Thus, the output will be:

  <w>Goo</w> <w>Joseph</w> <y>Lee</y> <x>Kenny</x> 

I know that we can "copy" from "select" the next siblings "from" z [1] "but I do not know how to stop it on" z [2] "

+4
source share
2 answers

Select the intersection of the following two node-sets:

  • All nodes after the first z
  • All nodes before the second z

The intersection of these two sets will be the entire node between the first and second elements of z . Use the following expression:

 /*/z/following-sibling::*[ count(.|/*/z[2]/preceding-sibling::*) = count(/*/z[2]/preceding-sibling::*)] 

Note The Kayessian node -set intersection formula is used here. In general, use the following to find the intersection of $set1 and $set2 :

 $set1[count(.|$set2)=count($set2)] 
+4
source

You can use XPath:

 text/*[preceding-sibling::z and following-sibling::z[2]] 

eg:.

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="/"> <xsl:apply-templates select="text/*[preceding-sibling::z and following-sibling::z[2]]"/> </xsl:template> </xsl:stylesheet> 
+2
source

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


All Articles