xsl-list
[Top] [All Lists]

Re: [xsl] General algorithm for finding nodes between PIs

2017-01-27 10:50:48
or sometimes it's easier with xslt- style sibling recursion rather
then using for-each-group

Not sure I have all the cases but

<x>
 <p><?Fm Condstart USB?>If you need this, do that.<?Fm Condend USB?>text<?Fm
 Condstart foo?>Some text<?Fm Condend foo?>another text</p>

 <p><?Fm Condstart USB?><span>If you need</span>
 <span>this, do that.</span><?Fm Condend USB?>text<?Fm
 Condstart foo?>Some text<?Fm Condend foo?><span>another text</span></p>
</x>


comes out as

<?xml version="1.0" encoding="UTF-8"?><x>
 <p platform="USB">If you need this, do that.textSome textanother text</p>

 <p><span platform="USB">If you need</span>
 <span platform="USB">this, do that.</span>textSome text<span>another
text</span></p>
</x>


using


<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>



 <xsl:template match="node()">
  <xsl:param name="att"/>
  <xsl:copy>
   <xsl:copy-of select="$att"/>
   <xsl:apply-templates select="child::node()[1]"/>
  </xsl:copy>
  <xsl:apply-templates select="following-sibling::node()[1]">
   <xsl:with-param name="att" select="$att"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="processing-instruction(Fm)">
  <xsl:param name="att"/>
  <xsl:choose>
   <xsl:when test="following-sibling::node()[1]/self::text() and
           not(preceding-sibling::node()) and
           starts-with(.,'Condstart ')">
    <xsl:attribute name="platform" select="substring-after(.,'Condstart ')"/>
      <xsl:apply-templates select="following-sibling::node()[1]"/>
   </xsl:when>
   <xsl:when test="following-sibling::node()[1]/self::*
           and starts-with(.,'Condstart ')">
    <xsl:apply-templates select="following-sibling::node()[1]">
     <xsl:with-param name="att" as="attribute()">
      <xsl:attribute name="platform" select="substring-after(.,'Condstart ')"/>
     </xsl:with-param>
    </xsl:apply-templates>
   </xsl:when>
   <xsl:when test="substring-after(.,'Condend ')=$att">
    <xsl:apply-templates select="following-sibling::node()[1]"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:apply-templates select="following-sibling::node()[1]">
     <xsl:with-param name="att" select="$att"/>
    </xsl:apply-templates>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

</xsl:stylesheet>
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>