为每个使用的html标签创建xslt

时间:2014-02-06 02:22:00

标签: javascript html css xml xslt

我正在尝试为我的html标签创建一个xslt,其中包含链接,图像和文本。 我尝试使用w3schools创建但无法创建它... 你能告诉我如何创造它吗? 在下面提供我的代码......

我的h,p和img标签是可重复的我需要把它放进去... 你能告诉我怎么做......

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>


      <xsl:for-each select="section/article/div">

      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

1 个答案:

答案 0 :(得分:0)

试试这个模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:key name="kImg" match="img" use="@src"/>

    <xsl:key name="kH" match="h1" use="concat(@class, '+', .)"/>

    <xsl:key name="kP" match="p" use="."/>

    <xsl:template match="/">
        <duplicates>
            <xsl:for-each select="//img[generate-id()=generate-id(key('kImg',@src)[2])]">
                <duplicate-images>
                    <xsl:copy-of select="key('kImg',@src)"/>
                </duplicate-images>
            </xsl:for-each>
            <xsl:for-each select="//h1[generate-id()=generate-id(key('kH',concat(@class, '+', .))[2])]">
                <duplicate-h1>
                    <xsl:copy-of select="key('kH',concat(@class, '+', .))"/>
                </duplicate-h1>
            </xsl:for-each>
            <xsl:for-each select="//p[generate-id()=generate-id(key('kP',.)[2])]">
                <duplicate-paragraphs>
                    <xsl:copy-of select="key('kP',.)"/>
                </duplicate-paragraphs>
            </xsl:for-each>
        </duplicates>
    </xsl:template>

</xsl:stylesheet>

使用您的JSFiddle输入,它输出:

<?xml version="1.0" encoding="utf-8"?>
<duplicates>
    <duplicate-images>
        <img src="/images/shop-landing/solutions.jpg"></img>
        <img src="/images/shop-landing/solutions.jpg"></img>
    </duplicate-images>
    <duplicate-h1>
        <h1 class="shop-features__subtitle">Attract Customers</h1>
        <h1 class="shop-features__subtitle">Attract Customers</h1>
    </duplicate-h1>
    <duplicate-paragraphs>
        <p> Find new ways to spread the word.<br></br> Expand your reach with the right<br></br>
            advice and tools to help keep<br></br> you ahead of the pack.<br></br>
            <a href="" class="button button--hero mini-configurator__button">Buy Now</a>
        </p>
        <p> Find new ways to spread the word.<br></br> Expand your reach with the right<br></br>
            advice and tools to help keep<br></br> you ahead of the pack.<br></br>
            <a href="" class="button button--hero mini-configurator__button">Buy Now</a>
        </p>
    </duplicate-paragraphs>
</duplicates>

改编自How to output duplicate elements using XSLT?