如何将每 3 个元素包装到一个 div 中,每个 xslt [英] How wrap every 3rd elements in to a div, xslt each

查看:29
本文介绍了如何将每 3 个元素包装到一个 div 中,每个 xslt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XSLT 的新手.这是我的 XML

<联系方式><人物><姓名>汉娜</姓名><电话>123-123-123</电话><address>No.1 A St. city</address><人物><人物><姓名>大卫</姓名><电话>223-223-223</电话><地址>No.2 B BRC.城市</地址><人物><人物><name>Tim</name><电话>323-223-333</电话><地址>No.3 c BRC.城市</地址><人物><人物><name>可能</name><电话>443-443-443</电话><地址>No.4 bb Rd.城市</地址><人物><人物><name>Rose</name><电话>7743-443-443</电话><地址>7号滚动路.城市</地址><人物>..........更多的<联系方式>

输出我正在尝试

<div class="信息"><h2>汉娜<h2/><h3>123-123-123</h3><p>No.1 A St. city<p/>

<div class="信息"><h2>大卫<h2/><h3>223-223-223</h3><p>No.2 B BRC.城市<p/>

<div class="信息"><h2>Tim<h2/><h3>323-223-333</h3><p>No.3c BRC.城市<p/>

<div class="group"><div class="信息"><h2>五月</h2><h3>443-443-443</h3><p>No.4 bb Rd.城市

<div class="信息"><h2>玫瑰</h2><h3>7743-443-443</h3><p>7号滚动路.城市

.....循环如果有更多

这是我所做的,但有很多错误.如何选择更多for-each select?

<xsl:param name="name"/><xsl:param name="电话"/><xsl:param name="地址"/><div class="信息"><h2><xsl:value-of select="$name"/></h2><h3><xsl:value-of select="$phone"/></h3><p><xsl:value-of select="$address"/></p>

</xsl:模板><xsl:template match="/*"><xsl:variable name="name" select="//name"/><xsl:variable name="phone" select="//phone"/><xsl:variable name="address" select="//address"/><xsl:for-each select="$name"><xsl:if test="(position() mod 4) = 1"><xsl:variable name="position" select="position()"/><div class="group"><xsl:call-template name="group"><xsl:with-param name="name" select="."/><xsl:with-param name="phone" select="."/><xsl:with-param name="address" select="."/></xsl:call-template>

</xsl:if></xsl:for-each></xsl:模板></xsl:stylesheet>

如果有人可以请纠正我的错误好吗?非常感谢

解决方案

如果有人可以请纠正我的错误吗?

我不知道我是否可以纠正您的错误,因为我真的不明白您的样式表要到哪里去.你似乎有正确的想法,这里需要一个处理模板,但之后我失去了你.

尝试以下方法:

<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/><xsl:template match="/"><根><xsl:call-template name="divide"><xsl:with-param name="items" select="contact/person"/></xsl:call-template></root></xsl:模板><xsl:template name="divide"><xsl:param name="items"/><xsl:param name="groupSize" select="3"/><xsl:param name="i" select="1"/><xsl:when test="$i > count($items)"/><xsl:否则><div class="group"><xsl:for-each select="$items[$i &lt;= position() and position() &lt; $i+$groupSize]"><div class="信息"><h2><xsl:value-of select="name"/></h2><h3><xsl:value-of select="phone"/></h3><p><xsl:value-of select="address"/></p>

</xsl:for-each>

<xsl:call-template name="divide"><xsl:with-param name="items" select="$items"/><xsl:with-param name="i" select="$i+$groupSize"/></xsl:call-template></xsl:否则></xsl:选择></xsl:模板></xsl:stylesheet>

请注意,我已将 元素添加到您所需的输出中缺少的输出中.当我在做的时候,你的输入也是无效的: 标签没有关闭.

I am quite newbie on XSLT. here is my XML

<contact>
 <person>
   <name>Hannah</name>
   <phone>123-123-123</phone>
   <address>No.1 A St. city</address>
 <person>

 <person>
   <name>David</name>
   <phone>223-223-223</phone>
   <address>No.2 B BRC. city</address>
 <person>

 <person>
   <name>Tim</name>
   <phone>323-223-333</phone>
   <address>No.3 c BRC. city</address>
 <person>

  <person>
   <name>May</name>
   <phone>443-443-443</phone>
   <address>No.4 bb Rd. city</address>
  <person>

  <person>
   <name>Rose</name>
   <phone>7743-443-443</phone>
   <address>No.7 rolling Rd. city</address>
  <person>

  ..........more
 <contact>

The OUTPUT I am trying to have

<div class="group">
    <div class="info">
        <h2>Hannah<h2/>
        <h3>123-123-123</h3>
        <p>No.1 A St. city<p/>
    </div>

    <div class="info">
        <h2>David<h2/>
        <h3>223-223-223</h3>
        <p>No.2 B BRC. city<p/>
    </div>

    <div class="info">
        <h2>Tim<h2/>
        <h3>323-223-333</h3>
        <p>No.3 c BRC. city<p/>
    </div>
</div>

<div class="group">
    <div class="info">
        <h2>May</h2>
        <h3>443-443-443</h3>
        <p>No.4 bb Rd. city</p>
    </div>

    <div class="info">
        <h2>Rose</h2>
        <h3>7743-443-443</h3>
        <p>No.7 rolling Rd. city</p>
    </div>

 ..... Loop if has more 
</div>

Here is what I did, but there are many errors. how to select more for-each select?

<xsl:template name="group">
    <xsl:param name="name"/>
    <xsl:param name="phone"/>
    <xsl:param name="address"/>
    <div class="info">
       <h2><xsl:value-of select="$name"/></h2>
       <h3><xsl:value-of select="$phone"/></h3>
       <p><xsl:value-of select="$address"/></p>
    </div>
</xsl:template>

  <xsl:template match="/*">

    <xsl:variable name="name" select="//name"/>
    <xsl:variable name="phone" select="//phone"/>
    <xsl:variable name="address" select="//address"/>

      <xsl:for-each select="$name">
        <xsl:if test="(position() mod 4) = 1">
          <xsl:variable name="position" select="position()"/>

          <div class="group">
            <xsl:call-template name="group">
              <xsl:with-param name="name" select="."/>
               <xsl:with-param name="phone" select="."/>
                <xsl:with-param name="address" select="."/>
            </xsl:call-template>

          </div>
        </xsl:if>
      </xsl:for-each>

  </xsl:template>
</xsl:stylesheet>

If someone could please correct my errors please? Many thanks

解决方案

If someone could please correct my errors please?

I don't know if I can correct your errors, because I don't really understand where you're going with your stylesheet. You seem to have the right idea that a processing template is required here, but I lost you after that.

Try the following approach:

<?xml version="1.0" encoding="utf-8"?>
<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:template match="/">
<root>
    <xsl:call-template name="divide">
        <xsl:with-param name="items" select="contact/person"/>
    </xsl:call-template>
</root>
</xsl:template>

<xsl:template name="divide">
    <xsl:param name="items" />
    <xsl:param name="groupSize" select="3"/>
    <xsl:param name="i" select="1"/>
    <xsl:choose>
        <xsl:when test="$i > count($items)"/>
        <xsl:otherwise>
            <div class="group">
                <xsl:for-each select="$items[$i &lt;= position() and position() &lt; $i+$groupSize]">
                    <div class="info">
                        <h2><xsl:value-of select="name"/></h2>
                        <h3><xsl:value-of select="phone"/></h3>
                        <p><xsl:value-of select="address"/></p>
                    </div>
                </xsl:for-each> 
            </div>
            <xsl:call-template name="divide">
                <xsl:with-param name="items" select="$items"/>
                <xsl:with-param name="i" select="$i+$groupSize"/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Note that I have added a <root> element to the output which is missing from your required output. While I am at it, your input is also invalid: the <person> tags are not closed.

这篇关于如何将每 3 个元素包装到一个 div 中,每个 xslt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆