XML: check if anything exists between two nodes

I need to find out if there is something between the two nodes. My XML looks like this:

<event value1="1" value2="2" value3="3" info="some info here">
          Line 1.<lb/>
          Line 2.<lb/><lb/>
          Line 3.<lb/>
          Line 4.<lb/>
</event>

My goal is to convert nodes <lb/>to <br/>HTML tags using XSLT. However, there is one more requirement. In case there is one <lb/> immediately following the other <lb/>, I want to output only one <br/> .

My XSLT looks like this:

 <xsl:template match="lb">
    <xsl:if test="not(preceding-sibling::lb[1])">
       <br/>
    </xsl:if>
 </xsl:template>

The problem with the above XSLT is that it only works correctly for line 1 if the text between the two nodes is ignored.

Maybe someone here can help.

+3
source share
3 answers

. , :

<xsl:template match="lb">
    <xsl:if test="following-sibling::node()[1][not(self::lb)]">
        <br/>
    </xsl:if>
</xsl:template>

:

 <xsl:template match="lb">
   <xsl:if test="generate-id()=generate-id(preceding-sibling::text()[1]/following-sibling::lb[1])">
     <br/>
   </xsl:if>
 </xsl:template>

, <lb/> node, , <br/>, <lb/> .

+3

- :

<!-- per default, <lb> gets removed (empty template)... -->
<xsl:template match="lb" />

<!-- ...unless it is directly preceded by a non-blank text node -->
<xsl:template match="lb[
  normalize-space(preceding-sibling::node()[1][self::text()]) != ''
]">
  <br />
</xsl:template>

<lb/>, .

<lb/>, ( node), . . , .

+3

:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="lb"/>

 <xsl:template match=
  "lb[not(following-sibling::node()[1][self::lb])]">
  <br />
 </xsl:template>
</xsl:stylesheet>

XML-:

<event value1="1" value2="2" value3="3" info="some info here">
          Line 1.<lb/>
          Line 2.<lb/><lb/>
          Line 3.<lb/>
          Line 4.<lb/>
</event>

:

<event value1="1" value2="2" value3="3" info="some info here">
          Line 1.<br/>
          Line 2.<br/>
          Line 3.<br/>
          Line 4.<br/>
</event>

:

  • Use and redefinition of identity transformation.

  • How items are deleted <lb>by default.

  • As soon as the last element <lb>in the group of neighboring sibling nodes is <lb>processed in a more special way, it is replaced by the element <br />.

+1
source

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


All Articles