代码之家  ›  专栏  ›  技术社区  ›  Michiel Borkent

删除节点副本中的某些节点

  •  0
  • Michiel Borkent  · 技术社区  · 7 年前

    div#about table

    输入HTML:

    <html>
      <body>
        <div class="content-header">
          <h1>Title</h1>
        </div>
        <div id="about">
          <h1>About</h1>
          <table>...</table>
          <p>Bla bla bla</p>
          <table>...</table>
          <p>The end</p>
        </div>
      </body>
    </html>
    

    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
            <div class="article">
                <h1>
                  <xsl:value select="//div[@class='content-header']/h1/text()"/>
                </h1>
    
                <div>
                    <xsl:copy-of select="//div[@id='about']"/>
                   <!-- Here should render the entire div#about without the tables -->
                </div>
    
            </div>
        </xsl:template>
    
        <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
    </xsl:transform>
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Tim C    7 年前

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    

    <xsl:mode on-no-match="shallow-copy"/>

    table

    <xsl:template match="div[@id='about']/table" />
    

    xsl:copy-of xsl:apply-templates 桌子

    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" />
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="div[@id='about']/table" />
    
        <xsl:template match="/">
            <div class="article">
                <h1>
                  <xsl:value-of select="//div[@class='content-header']/h1/text()"/>
                </h1>
                <div>
                    <xsl:apply-templates select="//div[@id='about']"/>
                </div>
            </div>
        </xsl:template>
    </xsl:transform>