rajat sethi
rajat sethi

Reputation: 57

Strip all the text from a specific node and remove all tags from xml using xslt1

I'm trying to strip all tags from a xml doc and i need to strip all text from a specific node only. For more clearity see the below example:

<root>
   <p>My 1st Semester Visual</p>
   <p>
      <b>Self Reflection</b>
   </p>
   <p>The activity</p>
   <content-block>
      <div class="imageWrapper" />
   </content-block>
   <p id="5fce699db97470099ea6c7e6">    </p>
   <content-block>
      <div class="carousel">
         <div class="carouselHeader" />
         <div class="carouselNavbar">
            <div class="carouselNavbarThumbnails" />
         </div>
      </div>
      My Space Unit Flyer
   </content-block>
   <div>
      <br />
   </div>
</root>

Result:

<root><text>My 1st Semester VisualSelf ReflectionThe activity
      My Space Unit Flyer
   </text><contentBlocks>2</contentBlocks></root>

Expected result: I also need to remove text that is inside the <content-block>.

<root><text>My 1st Semester VisualSelf ReflectionThe activity
   </text><contentBlocks>2</contentBlocks></root>

My xslt:

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html" encoding="UTF-8" indent="no" omit-xml-declaration="yes"/>

    <!-- Strip out white space -->
    <xsl:strip-space elements="*"/>

    <!-- Strip out all html tags, only leaving text contents -->
    <xsl:template match="*">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="root">
        <root>
            <text>
                <xsl:apply-templates/>
            </text>
            <contentBlocks>
                <xsl:if test="//content-block">
                    <xsl:value-of select="count(//content-block)"/>
                </xsl:if>
                <xsl:if test="figure">
                    <xsl:value-of select="count(figure)"/>
                </xsl:if>
            </contentBlocks>
        </root>
    </xsl:template>
</xsl:transform>

Thanks in advance

Upvotes: 0

Views: 104

Answers (1)

John Ernst
John Ernst

Reputation: 1235

<!-- Add this to your code. It suppresses content-block. -->
<xsl:template match="content-block"/>

Upvotes: 1

Related Questions