Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何将每3个元素包装到一个div中,xslt每个_Xml_Xslt - Fatal编程技术网

Xml 如何将每3个元素包装到一个div中,xslt每个

Xml 如何将每3个元素包装到一个div中,xslt每个,xml,xslt,Xml,Xslt,我是XSLT的新手。 这是我的XML <contact> <person> <name>Hannah</name> <phone>123-123-123</phone> <address>No.1 A St. city</address> <person> <person> <name>David</name> &l

我是XSLT的新手。 这是我的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>

如果有人能纠正我的错误,好吗?非常感谢

我不会对每个都使用
。以下样式表以更惯用的方式解决了您的问题:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- the number of items to include in each group -->
    <xsl:variable name="group" select="3" />
    <xsl:template match="/">
        <xsl:apply-templates select="contact/person[position() mod $group = 1]" />
    </xsl:template>
    <xsl:template match="person">
        <div class="group">
            <xsl:apply-templates
                select=".|following-sibling::person[position() &lt; $group]" 
                  mode="inner" />
        </div>
    </xsl:template>
    <xsl:template match="person" mode="inner">
        <div class="info">
            <xsl:apply-templates />
        </div>
    </xsl:template>
    <xsl:template match="name">
        <h2><xsl:apply-templates /></h2>
    </xsl:template>
    <xsl:template match="phone">
        <h3><xsl:apply-templates /></h3>
    </xsl:template>
    <xsl:template match="address">
        <p><xsl:apply-templates /></p>
    </xsl:template>
</xsl:stylesheet>


我们每隔三个人选择一个
人(从第一个开始),并对该元素及其下两个同级应用一个特殊模板(使用模式)。

我不会对每个
使用
。以下样式表以更惯用的方式解决了您的问题:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- the number of items to include in each group -->
    <xsl:variable name="group" select="3" />
    <xsl:template match="/">
        <xsl:apply-templates select="contact/person[position() mod $group = 1]" />
    </xsl:template>
    <xsl:template match="person">
        <div class="group">
            <xsl:apply-templates
                select=".|following-sibling::person[position() &lt; $group]" 
                  mode="inner" />
        </div>
    </xsl:template>
    <xsl:template match="person" mode="inner">
        <div class="info">
            <xsl:apply-templates />
        </div>
    </xsl:template>
    <xsl:template match="name">
        <h2><xsl:apply-templates /></h2>
    </xsl:template>
    <xsl:template match="phone">
        <h3><xsl:apply-templates /></h3>
    </xsl:template>
    <xsl:template match="address">
        <p><xsl:apply-templates /></p>
    </xsl:template>
</xsl:stylesheet>

我们每三分之一选择一个人(从第一个开始),并对该元素及其下两个同级应用一个特殊模板(使用模式)

如果有人能纠正我的错误,好吗

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

尝试以下方法:

<?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>

请注意,我在输出中添加了一个
元素,该元素在您所需的输出中缺失。在我进行此操作时,您的输入也无效:
标记未关闭

如果有人能纠正我的错误,好吗

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

尝试以下方法:

<?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>

请注意,我在输出中添加了一个
元素,该元素在您所需的输出中缺失。在我进行此操作时,您的输入也无效:
标记未关闭