使用xslt移动元素并重新编号一些id

时间:2015-12-02 00:31:26

标签: xml xslt xhtml

我需要在一个部分的末尾移动,所有具有某个类的p,我认为这个例子可能会澄清:

输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>MyPage</title>
  </head>
  <body>
      <section>
         <h1 class="title">Lorem ipsum</h1>
         <p class="text1">
             Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sollicitudin<sup><a id="note-211-backlink" class="note-link" href="#note-211">211</a></sup>  tristique massa, quis luctus sem congue fermentum<sup><a id="note-212-backlink" class="note-link" href="#note-212">212</a></sup>. Maecenas venenatis enim ligula. Vestibulum rutrum laoreet rhoncus. Suspendisse vitae semper purus, et tincidunt ex. Aliquam a tortor placerat<sup><a id="note-213-backlink" class="note-link" href="#note-213">213</a></sup>, porta lectus ac, condimentum dolor. Etiam id urna aliquet, viverra urna id, efficitur dolor. Fusce consectetur, erat vitae sollicitudin malesuada, urna nisi imperdiet ligula, scelerisque porta sem orci non leo. Nulla vitae consequat enim<sup><a id="note-214-backlink" class="note-link" href="#note-214">214</a></sup>. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam a iaculis metus.
         </p>
         <div class="inline-notes">
            <p class="note"><a id="note-211" class="note-anchor" href="#note-211-backlink">211</a> Cras id nibh et lacus pulvinar hendrerit eget vel massa.
            </p>
            <p class="note"><a id="note-212" class="note-anchor" href="#note-212-backlink">212</a> Fusce quis metus ac nisl lacinia porta.
            </p>
            <p class="note"><a id="note-213" class="note-anchor" href="#note-213-backlink">213</a> Phasellus vestibulum volutpat ipsum.
            </p>
            <p class="note"><a id="note-214" class="note-anchor" href="#note-214-backlink">214</a> Vestibulum eu libero non lorem vulputate scelerisque.
            </p>
         </div>
         <p class="text2">Nullam ipsum enim, laoreet quis neque suscipit<sup><a id="note-215-backlink" class="note-link" href="#note-215">215</a></sup>, ornare sodales orci. Curabitur fringilla ornare ullamcorper. Pellentesque eu sem leo<sup><a id="note-216-backlink" class="note-link" href="#note-216">216</a></sup>. Cras eu consectetur mi, ac sagittis diam. Nunc at odio ac arcu mollis pharetra.
         </p>
         <div class="inline-notes">
            <p class="note"><a id="note-215" class="note-anchor" href="#note-215-backlink">215</a> Praesent bibendum lacus ac dolor eleifend porta.
            </p>
            <p class="note"><a id="note-216" class="note-anchor" href="#note-216-backlink">216</a> Phasellus vestibulum volutpat ipsum.
            </p>
         </div>
      </section>
   </body>
</html>

期望的结果:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>MyPage</title>
  </head>
  <body>
      <section>
         <h1 class="title">Lorem ipsum</h1>
         <p class="text1">
             Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sollicitudin<sup><a id="note-1-backlink" class="note-link" href="#note-1">1</a></sup>  tristique massa, quis luctus sem congue fermentum<sup><a id="note-2-backlink" class="note-link" href="#note-2">2</a></sup>. Maecenas venenatis enim ligula. Vestibulum rutrum laoreet rhoncus. Suspendisse vitae semper purus, et tincidunt ex. Aliquam a tortor placerat<sup><a id="note-3-backlink" class="note-link" href="#note-3">3</a></sup>, porta lectus ac, condimentum dolor. Etiam id urna aliquet, viverra urna id, efficitur dolor. Fusce consectetur, erat vitae sollicitudin malesuada, urna nisi imperdiet ligula, scelerisque porta sem orci non leo. Nulla vitae consequat enim<sup><a id="note-4-backlink" class="note-link" href="#note-4">4</a></sup>. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam a iaculis metus.
         </p>
         <p class="text2">Nullam ipsum enim, laoreet quis neque suscipit<sup><a id="note-5-backlink" class="note-link" href="#note-5">5</a></sup>, ornare sodales orci. Curabitur fringilla ornare ullamcorper. Pellentesque eu sem leo<sup><a id="note-6-backlink" class="note-link" href="#note-6">6</a></sup>. Cras eu consectetur mi, ac sagittis diam. Nunc at odio ac arcu mollis pharetra.
         </p>
         <div class="note-container">
            <p class="note"><a id="note-1" class="note-anchor" href="#note-1-backlink">1</a> Cras id nibh et lacus pulvinar hendrerit eget vel massa.
            </p>
            <p class="note"><a id="note-2" class="note-anchor" href="#note-2-backlink">2</a> Fusce quis metus ac nisl lacinia porta.
            </p>
            <p class="note"><a id="note-3" class="note-anchor" href="#note-3-backlink">3</a> Phasellus vestibulum volutpat ipsum.
            </p>
            <p class="note"><a id="note-4" class="note-anchor" href="#note-4-backlink">4</a> Vestibulum eu libero non lorem vulputate scelerisque.
            </p>
            <p class="note"><a id="note-5" class="note-anchor" href="#note-5-backlink">5</a> Praesent bibendum lacus ac dolor eleifend porta.
            </p>
            <p class="note"><a id="note-6" class="note-anchor" href="#note-6-backlink">6</a> Phasellus vestibulum volutpat ipsum.
            </p>
         </div>
      </section>

这是我的xslt:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:h="http://www.w3.org/1999/xhtml"
    xmlns:gb="http://www.digitaltypes.it"
    exclude-result-prefixes="xs h gb #default"
    version="2.0">

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

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

    <xsl:template match="p[@class='note']"/>
    <xsl:template match="//div[@class='note_container']">
        <xsl:apply-templates/>
        <xsl:copy-of select="//p[contains(@class,'note')]"/>
    </xsl:template>
</xsl:stylesheet>

我尝试了几种可能性,但没有人为我工作。我无法理解如何在我可以创建的div note-container中移动各种类的笔记。现在重要的是重新编号,我想知道如何移动这个标签。

谢谢你。

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你想做:

XSLT 2.0

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

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

<xsl:template match="h:section">
    <xsl:copy>
        <xsl:apply-templates select="@*|node() except h:div[@class='inline-notes']"/>
        <div class="note_container">
            <xsl:apply-templates select="h:div[@class='inline-notes']/h:p[@class='note']"/>
        </div>
    </xsl:copy>
</xsl:template>

<xsl:template match="h:p[@class='note']">
    <xsl:variable name="i" select="position()" />
    <xsl:copy>
        <a id="note-{$i}" class="note-anchor" href="#note-{$i}-backlink">
            <xsl:value-of select="$i"/>
        </a> 
        <xsl:value-of select="text()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

请注意使用h:前缀以及默认命名空间声明。

答案 1 :(得分:0)

类似于Michaels的答案,但也处理note-link

XML输入

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>MyPage</title>
    </head>
    <body>
        <section>
            <h1 class="title">Lorem ipsum</h1>
            <p class="text1">
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sollicitudin<sup><a id="note-211-backlink" class="note-link" href="#note-211">211</a></sup>  tristique massa, quis luctus sem congue fermentum<sup><a id="note-212-backlink" class="note-link" href="#note-212">212</a></sup>. Maecenas venenatis enim ligula. Vestibulum rutrum laoreet rhoncus. Suspendisse vitae semper purus, et tincidunt ex. Aliquam a tortor placerat<sup><a id="note-213-backlink" class="note-link" href="#note-213">213</a></sup>, porta lectus ac, condimentum dolor. Etiam id urna aliquet, viverra urna id, efficitur dolor. Fusce consectetur, erat vitae sollicitudin malesuada, urna nisi imperdiet ligula, scelerisque porta sem orci non leo. Nulla vitae consequat enim<sup><a id="note-214-backlink" class="note-link" href="#note-214">214</a></sup>. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam a iaculis metus.
            </p>
            <div class="inline-notes">
                <p class="note"><a id="note-211" class="note-anchor" href="#note-211-backlink">211</a> Cras id nibh et lacus pulvinar hendrerit eget vel massa.
                </p>
                <p class="note"><a id="note-212" class="note-anchor" href="#note-212-backlink">212</a> Fusce quis metus ac nisl lacinia porta.
                </p>
                <p class="note"><a id="note-213" class="note-anchor" href="#note-213-backlink">213</a> Phasellus vestibulum volutpat ipsum.
                </p>
                <p class="note"><a id="note-214" class="note-anchor" href="#note-214-backlink">214</a> Vestibulum eu libero non lorem vulputate scelerisque.
                </p>
            </div>
            <p class="text2">Nullam ipsum enim, laoreet quis neque suscipit<sup><a id="note-215-backlink" class="note-link" href="#note-215">215</a></sup>, ornare sodales orci. Curabitur fringilla ornare ullamcorper. Pellentesque eu sem leo<sup><a id="note-216-backlink" class="note-link" href="#note-216">216</a></sup>. Cras eu consectetur mi, ac sagittis diam. Nunc at odio ac arcu mollis pharetra.
            </p>
            <div class="inline-notes">
                <p class="note"><a id="note-215" class="note-anchor" href="#note-215-backlink">215</a> Praesent bibendum lacus ac dolor eleifend porta.
                </p>
                <p class="note"><a id="note-216" class="note-anchor" href="#note-216-backlink">216</a> Phasellus vestibulum volutpat ipsum.
                </p>
            </div>
        </section>
    </body>
</html>

XSLT 2.0

<xsl:stylesheet version="2.0" 
  xmlns="http://www.w3.org/1999/xhtml"
  xpath-default-namespace="http://www.w3.org/1999/xhtml" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" method="xml"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="section">
    <xsl:copy>
      <xsl:apply-templates select="@*|node() except div[tokenize(@class,'\s')='inline-notes']"/>
      <div class="note-container">
        <xsl:apply-templates select="div[tokenize(@class,'\s')='inline-notes']/*"/>
      </div>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="a[tokenize(@class,'\s')='note-anchor']">
    <xsl:variable name="noteNbr">
      <xsl:number count="a[tokenize(@class,'\s')='note-anchor']" level="any"/>
    </xsl:variable>
    <a id="{concat('note-',$noteNbr)}" href="{concat('#note-',$noteNbr,'-backlink')}">
      <xsl:apply-templates select="@* except (@href,@id),$noteNbr"/>
    </a>
  </xsl:template>

  <xsl:template match="a[tokenize(@class,'\s')='note-link']">
    <xsl:variable name="noteNbr" select="index-of(//a[tokenize(@class,'\s')='note-anchor']/@id,substring-after(@href,'#'))"/>
    <a id="{concat('note-',$noteNbr,'-backlink')}" href="{concat('#note-',$noteNbr)}">
      <xsl:apply-templates select="@* except (@id,@href)"/>
      <xsl:value-of select="$noteNbr"/>
    </a>
  </xsl:template>

</xsl:stylesheet>

XML输出

<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <title>MyPage</title>
   </head>
   <body>
      <section>
         <h1 class="title">Lorem ipsum</h1>
         <p class="text1">
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sollicitudin<sup>
               <a id="note-1-backlink" href="#note-1" class="note-link">1</a>
            </sup>  tristique massa, quis luctus sem congue fermentum<sup>
               <a id="note-2-backlink" href="#note-2" class="note-link">2</a>
            </sup>. Maecenas venenatis enim ligula. Vestibulum rutrum laoreet rhoncus. Suspendisse vitae semper purus, et tincidunt ex. Aliquam a tortor placerat<sup>
               <a id="note-3-backlink" href="#note-3" class="note-link">3</a>
            </sup>, porta lectus ac, condimentum dolor. Etiam id urna aliquet, viverra urna id, efficitur dolor. Fusce consectetur, erat vitae sollicitudin malesuada, urna nisi imperdiet ligula, scelerisque porta sem orci non leo. Nulla vitae consequat enim<sup>
               <a id="note-4-backlink" href="#note-4" class="note-link">4</a>
            </sup>. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam a iaculis metus.
            </p>
         <p class="text2">Nullam ipsum enim, laoreet quis neque suscipit<sup>
               <a id="note-5-backlink" href="#note-5" class="note-link">5</a>
            </sup>, ornare sodales orci. Curabitur fringilla ornare ullamcorper. Pellentesque eu sem leo<sup>
               <a id="note-6-backlink" href="#note-6" class="note-link">6</a>
            </sup>. Cras eu consectetur mi, ac sagittis diam. Nunc at odio ac arcu mollis pharetra.
            </p>
         <div class="note-container">
            <p class="note">
               <a id="note-1" href="#note-1-backlink" class="note-anchor">1</a> Cras id nibh et lacus pulvinar hendrerit eget vel massa.
                </p>
            <p class="note">
               <a id="note-2" href="#note-2-backlink" class="note-anchor">2</a> Fusce quis metus ac nisl lacinia porta.
                </p>
            <p class="note">
               <a id="note-3" href="#note-3-backlink" class="note-anchor">3</a> Phasellus vestibulum volutpat ipsum.
                </p>
            <p class="note">
               <a id="note-4" href="#note-4-backlink" class="note-anchor">4</a> Vestibulum eu libero non lorem vulputate scelerisque.
                </p>
            <p class="note">
               <a id="note-5" href="#note-5-backlink" class="note-anchor">5</a> Praesent bibendum lacus ac dolor eleifend porta.
                </p>
            <p class="note">
               <a id="note-6" href="#note-6-backlink" class="note-anchor">6</a> Phasellus vestibulum volutpat ipsum.
                </p>
         </div>
      </section>
   </body>
</html>

注意:我使用了tokenize(@class,'\s'),以防你有一个具有多个值的class属性。

修改OP的评论So i have a question there is a way, via xslt, to apply the xslt only when exist the <div class="inline-notes">??

div不存在时,您可以添加另一个覆盖根元素的模板:

<xsl:template match="/*[not(//div[tokenize(@class,'\s')='inline-notes'])]">
  <xsl:copy-of select="."/>
</xsl:template>

或者,您可以将注释容器div包装在xsl:if

<xsl:template match="section">
  <xsl:copy>
    <xsl:apply-templates select="@*|node() except div[tokenize(@class,'\s')='inline-notes']"/>
    <xsl:if test="div[tokenize(@class,'\s')='inline-notes']">
      <div class="note-container">
        <xsl:apply-templates select="div[tokenize(@class,'\s')='inline-notes']/*"/>
      </div>
    </xsl:if>
  </xsl:copy>
</xsl:template>