Finding whitespace or text between a node and its next sibling using XSLT

I have an XML document that contains the following extract example:

<p>
    Some text <GlossaryTermRef href="123">term 1</GlossaryTermRef><GlossaryTermRef href="345">term 2</GlossaryTermRef>.
</p>

I use XSLT to convert this to XHTML using the following template:

<xsl:template match="GlossaryTermRef">
    <a href="#{@href}" class="glossary">
        <xsl:apply-templates select="node()|text()"/>
    </a>
</xsl:template>

This works pretty well, however do I need to insert a space between the two elements GlossaryTermRefif they appear next to each other?

Is there a way to determine if there is a space or text between the current node and the next-sibling? I can’t always insert a space GlossaryTermRef, as it can be followed by a punctuation mark.

+3
source share
3 answers

I managed to solve this problem by modifying the template as follows:

<xsl:template match="GlossaryTermRef">
    <a href="#{@href}" class="glossary">
        <xsl:apply-templates select="node()|text()"/>
    </a>
    <xsl:if test="following-sibling::node()[1][self::GlossaryTermRef]">
        <xsl:text> </xsl:text>
    </xsl:if>
</xsl:template>

- - ?

+3

-, "node() | text()" "node()". , "* | node()", , PI.

, , , . :

<xsl:for-each-group select="node()" group-adjacent="boolean(self::GlossaryTermRef)">
  <xsl:choose>
    <xsl:when test="current-grouping-key()">
      <xsl:for-each select="current-group()">
        <xsl:if test="position() gt 1"><xsl:text> </xsl:text></xsl:if>
        <xsl:apply-templates select="."/>
      </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
     <xsl:apply-templates select="current-group()"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:for-each-group>

, .

, sibling ( , ), , .

+2

? ?

<xsl:template match="GlossaryTermRef">
<a href="#{@href}" class="glossary">
<xsl:apply-templates select="node()|text()"/>
</a>
</xsl:template>
0

All Articles