Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem using the xsl method for-each

Tags:

xml

xslt

Using XSL I am trying to turn this XML:

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

into 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 output is looking like this:

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

Can anyone explain why the root text of <title> is getting lost? I believe my for-each select statement may need to be modified but I can't figure out what is should be.

Keep in mind that I cannot use an <xsl:template match> because of the complexity of my style sheet.

Thanks!

like image 779
joe Avatar asked Sep 13 '26 05:09

joe


1 Answers

This XPath expression:

book/title/*

means "all child elements of book/title". In your case, book/title has 3 child nodes:

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

As you can see, only one of them is an element, and gets selected. If you want to get all child nodes, both text and elements, use this:

book/title/node()

If you want to get text nodes separately, use this:

book/title/text()
like image 93
Pavel Minaev Avatar answered Sep 16 '26 18:09

Pavel Minaev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!