Problem using xsl method for each

Using XSL I'm trying to transform this XML:

<book><title>This is a <b>great</b> book</title></book>

in this xml:

<book>This is a <bold>great</bold> book</book>

using this xsl:

<xsl:for-each select="book/title/*">
<xsl:choose>
    <xsl:when test="name() = 'b'">
        <bold>
            <xsl:value-of select="text()"/>
        </bold>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="text()"/>
    </xsl:otherwise>
</xsl:choose>
</xsl:for-each>

but my conclusion is as follows:

<book><bold>great</bold></bold>

Can someone explain why the root text <title>is lost? I believe that my request for each request may need to be changed, but I cannot understand what should be.

Keep in mind that I cannot use <xsl:template match>due to the complexity of my stylesheet.

Thanks!

+3
source share
2 answers

This is an XPath expression:

book/title/*

means "all children book/title". In your case, it book/titlehas 3 child nodes:

  • Text node: This is a
  • Node element: <b>...</b>
  • Text node: book

, . , , , :

book/title/node()

, :

book/title/text()
+7

, , (, - ) XSLT-.

, XSLT:

XML-:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="title">
      <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="title/b">
      <bold>
       <xsl:apply-templates/>
      </bold>
    </xsl:template>
</xsl:stylesheet>

:

<book><title>This is a <b>great</b> book</title></book>

XSLT - / elment.

+1

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


All Articles