简体   繁体   中英

XSLT2 - Get text of all nodes except certain ones

I'm trying to get the text of a whole HTML looking document but I would like to exclude certain nodes from the retrieval.

XML

<body>
  <p>some text</p>
  <p>some text <img src="whatever"><alt>alt title</alt></p>
  <div>
     <p>some text</p>
  </div>
</body>

Context

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xsl:template match="/">

   <html>
     <body>
       <table>
         <xsl:for-each select="collection(...)"> 
           <xsl:variable name="text" select="string(//body)" />
              <tr>
                 <td><xsl:value-of select="$text"/></td>
              </tr>
          </xsl:for-each>
        <table>
      </body>
   </html>

  </xsl:template>
</stylesheet>

Expected result

I want everything but the alt element so some text *3 = 29 in this example.

I am doing this operation from a for-each on a collection of file. So right now I just do a string(/body) to get everything in each of my file.

I was thinking of a recursive call:

 <xsl:function name="lp:gettext">
    <xsl:param name="n" />
    <xsl:choose>
      <xsl:when test="$n/child::node()">
        <xsl:value-of select="concat(text(),lp:gettext($n/child::node()))" />
      </xsl:when>
      <xsl:when test="$n/name()='alt'" />
      <xsl:otherwise>
        <xsl:value-of select="text()" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:function>

But I can't seem to use child:: in a function it seems, or not like I do.

How can I achieve what I want to do?

Applying the following stylesheet:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="alt"/>

</xsl:stylesheet>

to your input (after closing the img tag!) returns:

<?xml version="1.0" encoding="UTF-8"?>
some textsome text some text

This is because the built-in template rules copy all text nodes to the output - so all you need to do is override the default behaviour for the nodes you don't want to be copied.


Added:

Based on the stylesheet you have added to your question, I believe the following should do what you want (I cannot test it):

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <html>
        <body>
            <table>
                <tr>
                    <td>
                        <xsl:apply-templates select="collection(...)"/>
                    </td>
                </tr>
            <table>
        </body>
    </html>
</xsl:template>

<xsl:template match="alt"/>

</xsl:stylesheet>

好的,我只是发现了这个remove-elements-deep函数,似乎完全可以实现我想要的功能。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM