P
P
Programep2016-12-22 14:02:23
XSL & XSLT
Programep, 2016-12-22 14:02:23

Is it possible to solve this problem with xslt template?

There is an xml structure:

<lev1>
 <lev2_1>
   <lev3_1>text...</lev3_1>
   <lev3_2>text...</lev3_2>
 </lev2_1>
 <lev2_2>
   <lev3_3>text...</lev3_3>
   <lev3_4>
    <lev4_1>need_search_text_here</lev4_1>
    <lev4_tag1>text</lev4_tag1>
    <lev4_tag2>text</lev4_tag2>
   </lev3_4>
  </lev2_2>
</lev1>

Goal: Keep only elements and their parents if the element contains the search text.
For some reason, this option copies elements without tags, i.e. only the contents of the elements:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="our_statistic" select="'search_text'"/>

  <xsl:template match="lev3_4">
        <xsl:choose>
            <xsl:when test="string-length(text()) = string-length(translate(text(),$our_statistic,''))">
              <xsl:copy-of select="../../node()" />
            </xsl:when>
        </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question