使用XSLT分页将XML转换为HTML [英] Convert XML to HTML using XSLT Pagination

查看:95
本文介绍了使用XSLT分页将XML转换为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有一些问题,我尝试了很多解决方案,但仍然遇到困难. 我试图在xsl中使用此分页 并且是否可以使用XSLT将XML分成多个页面?但没有得到我需要的东西,

I know there are some questions about this here, and I tried a lot of solutions around but i'm still stuck. I tried to use this pagination in xsl and this Possible to split XML into multiple pages with XSLT? but don't get what I need,

我需要通过XSLT解析器使用XML在生成的html中创建分页.

I need to create a pagination in my generated html, using a XML through a XSLT parser.

XML文件就是这样

<root>
    <result>
        <a>field one</a>
        <b>field two</b>
        <c>field three</c>
    </result>
    <result>
        <a>hello</a>
        <b>thanks</b>
        <c>for help</c>
    </result>
    <result>
        <a>thank</a>
        <b>you</b>
        <c>!!</c>
    </result>
    ..... 
</root>

我的没有分页的XSLT是

and my XSLT without pagination is this

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="http://mycompany.com/mynamespace">


    <xsl:output method="html" indent="yes" />

    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates />
            </body>
        </html>
    </xsl:template>

    <xsl:template match="root">
                    <xsl:for-each select="result">
                        <div class="list_item">
                            <div class="dmv_line">
                                <div class="box_text">
                                    <h2><xsl:value-of select="a"/></h2>
                                </div>
                                </div>
                                <div class="dmv_line">
                                    <div class="box_text">
                                        <h2><xsl:value-of select="b"/></h2>
                                    </div>
                                </div>
                                <div class="dmv_line">
                                    <div class="box_text">
                                        <h2><xsl:value-of select="c"/></h2>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </xsl:for-each>
    </xsl:template>


</xsl:stylesheet>

我需要创建一个<div>,将结果3×3或4×4封装起来,稍后我将使其隐藏或可见. 谢谢

What I need is to create a <div> that encapsulate results 3 by 3 or 4 by 4, which I will make hide or visible in javascript later.. Thanks

: HTML输出,如果我假设每页分页2个元素,并且仅考虑示例中的3个元素,则输出应为

: The HTML output, if I assume to make pagination 2 elements per page, and considering only the 3 elements in example, the output should be

<-- page number one --->
<div id="1">
 <div class="list_item">
     <div class="dmv_line">
        <div class="box_text">
            <h2>field one</h2>
        </div>
     </div>
    <div class="dmv_line">
        <div class="box_text">
            <h2>field two</h2>
        </div>
     </div>
     <div class="dmv_line">
        <div class="box_text">
            <h2>field three</h2>
        </div>
     </div>
    </div>
     <div class="list_item">
         <div class="dmv_line">
            <div class="box_text">
                <h2>hello</h2>
            </div>
         </div>
        <div class="dmv_line">
            <div class="box_text">
                <h2>thanks</h2>
            </div>
         </div>
         <div class="dmv_line">
            <div class="box_text">
                <h2>for help</h2>
            </div>
         </div>
    </div>
</div>
 <-- END OF page number one --->
 <-- page number two --->
<div id="2">     
     <div class="list_item">
         <div class="dmv_line">
            <div class="box_text">
                <h2>thank</h2>
            </div>
         </div>
        <div class="dmv_line">
            <div class="box_text">
                <h2>you</h2>
            </div>
         </div>
         <div class="dmv_line">
            <div class="box_text">
                <h2>!!!</h2>
            </div>
         </div>
    </div>
</div>  
 <-- END OF page number two --->

推荐答案

对于此类问题,方法是首先选择将在每个页面"上首先出现的元素.这可以通过检查其位置来实现.

For problems like this, the approach is to firstly select the elements that will occur first on each of your 'pages'. This is achieved by checking its position.

<xsl:for-each select="result[position() mod $size = 1]">

因此,如果 $ size 变量设置为2,则会在位置1、3、5等位置选择结果元素...

So, if the $size variable is set to 2, this would select the result elements in positions 1, 3, 5, etc...

在此循环中,您可以输出包含年龄"的 div 标签

Within this loop, you can then output the containing div tag for the 'age'

<div id="{position()}">  

请注意,位置是上下文相关的,并且将返回该节点在您刚刚选择的节点集中的位置(即它将返回1、2、3等)

Note that position is context-sensitive, and will return the position of the node in the node set you have just selected (i.e. it will return 1, 2, 3, etc)

最后,要获取构成页面"的结果元素,您可以像这样选择它们

Finally, to get the result elements that make up the 'page' you can select them like so

<xsl:apply-templates select="self::*|following-sibling::*[position() &lt; $size]" />

与它们匹配的模板实际上就像您在 xsl:for-each 中的现有代码一样.

And the template that matches them would effectively be like your existing code in the xsl:for-each.

尝试以下XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:param name="size" select="2"/>

   <xsl:output method="html" indent="yes"/>

   <xsl:template match="/">
      <html>
         <body>
            <xsl:apply-templates/>
         </body>
      </html>
   </xsl:template>

   <xsl:template match="root">
      <xsl:for-each select="result[position() mod $size = 1]">
         <div id="{position()}">
            <xsl:apply-templates select="self::*|following-sibling::*[position() &lt; $size]"/>
         </div>
      </xsl:for-each>
   </xsl:template>

   <xsl:template match="result">
      <div class="list_item">
         <div class="dmv_line">
            <div class="box_text">
               <h2>
                  <xsl:value-of select="a"/>
               </h2>
            </div>
         </div>
         <div class="dmv_line">
            <div class="box_text">
               <h2>
                  <xsl:value-of select="b"/>
               </h2>
            </div>
         </div>
         <div class="dmv_line">
            <div class="box_text">
               <h2>
                  <xsl:value-of select="c"/>
               </h2>
            </div>
         </div>
      </div>
   </xsl:template>
</xsl:stylesheet>

这有望为您提供所需的输出.

This should hopefully give you the output you need.

顺便说一句,您的代码中有一些代码重复.尝试使用以下两个模板替换与结果匹配的模板:

As an aside, you have a bit of code repetition in your code. Try replacing the template that matches result with these two templates instead:

<xsl:template match="result">
   <div class="list_item"> 
      <xsl:apply-templates select="a|b|c" />
   </div>
</xsl:template>

<xsl:template match="result/*">
      <div class="dmv_line">
         <div class="box_text">
            <h2>
               <xsl:value-of select="."/>
            </h2>
         </div>
       </div>
</xsl:template>

这篇关于使用XSLT分页将XML转换为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆