Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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
C# XSLT计算位置并相应地对项目进行分组_C#_Html_Xslt_Xslt 1.0_Xslt 2.0 - Fatal编程技术网

C# XSLT计算位置并相应地对项目进行分组

C# XSLT计算位置并相应地对项目进行分组,c#,html,xslt,xslt-1.0,xslt-2.0,C#,Html,Xslt,Xslt 1.0,Xslt 2.0,我有一些输入XML,它是 <collection> <content> <id>10</id> <type>xx</type> <title>xx</title> <quicklink>xx</quicklink> <teaser><p>xxx</p>&l

我有一些输入XML,它是

<collection>
    <content>
        <id>10</id>
        <type>xx</type>
        <title>xx</title>
        <quicklink>xx</quicklink>
        <teaser><p>xxx</p></teaser>
        <root>
            <thumb><img src="/xxx/xxx.jpg" /></thumb>
            <link>http://www.foo.com</link>
        </root>
        <startDate></startDate>
        <enddate></enddate>
        <hyperlink><a href="http://www.foo.com">some text</a></hyperlink>
    </content>
    <content>
        <id>10</id>
        <type>xx</type>
        <title>xx</title>
        <quicklink>xx</quicklink>
        <teaser><p>xxx</p></teaser>
        <root>
            <thumb><img src="/xxx/xxx.jpg" /></thumb>
            <link>http://www.foo.com</link>
        </root>
        <startDate></startDate>
        <enddate></enddate>
        <hyperlink><a href="http://www.foo.com">some text</a></hyperlink>
    </content>
    <content>
        <id>10</id>
        <type>xx</type>
        <title>xx</title>
        <quicklink>xx</quicklink>
        <teaser><p>xxx</p></teaser>
        <root>
            <thumb><img src="/xxx/xxx.jpg" /></thumb>
            <link>http://www.foo.com</link>
        </root>
        <startDate></startDate>
        <enddate></enddate>
        <hyperlink><a href="http://www.foo.com">some text</a></hyperlink>
    </content>
</collection>

我确实尝试过自己使用
position()
来实现这一点,但我的逻辑似乎不起作用。有人能看一下吗?非常感谢。

选择每三个
内容
元素,然后启动
li
元素,然后在该元素中选择所需的三个元素。不清楚您的
内容
元素是否是兄弟元素,因此我使用
跟随
而不是
跟随兄弟元素

  <xsl:for-each select="(Collection/Content)[position() mod 3 = 1]">
    <li>
       <xsl:for-each select=".|following::Content[position() &lt; 3]">
            <a href="{Html/root/Link}" title="{Title}">
               <img src="{Html/root/Thumb/img/@src}" alt="{Title}/>
             </a>
        </xsl:for-each>
   </li>
 </xsl:for-each>


  • 选择每三个
    内容
    元素,然后启动
    li
    元素,然后在其中选择所需的三个元素。不清楚您的
    内容
    元素是否是兄弟元素,因此我使用
    跟随
    而不是
    跟随兄弟元素

      <xsl:for-each select="(Collection/Content)[position() mod 3 = 1]">
        <li>
           <xsl:for-each select=".|following::Content[position() &lt; 3]">
                <a href="{Html/root/Link}" title="{Title}">
                   <img src="{Html/root/Thumb/img/@src}" alt="{Title}/>
                 </a>
            </xsl:for-each>
       </li>
     </xsl:for-each>
    
    
    

  • 好心的大卫为我指出了正确的方向,我成功地实现了正确的转变

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
        <ul id="case-study-icons">
          <xsl:for-each select="(Collection/Content)[position() mod 3 = 1]">
            <li style="width:100px; float:left;">
            <xsl:for-each select=".|following-sibling::Content[position() &lt; 3]">
              <a>
                <xsl:attribute name="href">
                  <xsl:value-of select="Html/root/Link"/>
                </xsl:attribute>
                <xsl:attribute name="title">
                  <xsl:value-of select="Title"/>
                </xsl:attribute>
                <img>
                  <xsl:attribute name="src">
                    <xsl:value-of select="Html/root/Thumb/img/@src"/>
                  </xsl:attribute>
                  <xsl:attribute name="alt">
                    <xsl:value-of select="Title"/>
                  </xsl:attribute>
                  <xsl:attribute name="width">92</xsl:attribute>
                  <xsl:attribute name="height">46</xsl:attribute>
                </img>
              </a>
            </xsl:for-each>
            </li>
          </xsl:for-each>
          </ul>
      </xsl:template>
    </xsl:stylesheet>
    
    
    
      宽度:100px;浮动:左;“>

    好心的大卫为我指出了正确的方向,我成功地实现了正确的转变

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
        <ul id="case-study-icons">
          <xsl:for-each select="(Collection/Content)[position() mod 3 = 1]">
            <li style="width:100px; float:left;">
            <xsl:for-each select=".|following-sibling::Content[position() &lt; 3]">
              <a>
                <xsl:attribute name="href">
                  <xsl:value-of select="Html/root/Link"/>
                </xsl:attribute>
                <xsl:attribute name="title">
                  <xsl:value-of select="Title"/>
                </xsl:attribute>
                <img>
                  <xsl:attribute name="src">
                    <xsl:value-of select="Html/root/Thumb/img/@src"/>
                  </xsl:attribute>
                  <xsl:attribute name="alt">
                    <xsl:value-of select="Title"/>
                  </xsl:attribute>
                  <xsl:attribute name="width">92</xsl:attribute>
                  <xsl:attribute name="height">46</xsl:attribute>
                </img>
              </a>
            </xsl:for-each>
            </li>
          </xsl:for-each>
          </ul>
      </xsl:template>
    </xsl:stylesheet>
    
    
    
      宽度:100px;浮动:左;“>

    您可能希望参数化每组的项目数,而不是硬编码3。此外,还可以简化它,首先避免为每个使用嵌套的xsl:for,其次使用属性值模板写出属性

    例如,试试这个XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:param name="itemsperlist" select="3" />
    
       <xsl:template match="/collection">
          <ul id="case-study-icons">
             <xsl:apply-templates select="content[(position() - 1) mod $itemsperlist = 0]" mode="first" />
          </ul>
       </xsl:template>
    
       <xsl:template match="content" mode="first">
          <li>
             <xsl:apply-templates select=".|following-sibling::content[position() &lt; $itemsperlist]" />
          </li>
       </xsl:template>
    
       <xsl:template match="content">
          <a href="{root/link}" title="{title}">
             <img src="{root/thumb/img/@src}" alt="{title}" width="92" height="46" />
          </a>
       </xsl:template>
    </xsl:stylesheet>
    
    
    

  • 请注意,如果将3更改为1,则此解决方案将起作用。您可能希望参数化每组的项目数,而不是硬编码3。此外,还可以简化它,首先避免为每个
    使用嵌套的xsl:for,其次使用属性值模板写出属性

    例如,试试这个XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:param name="itemsperlist" select="3" />
    
       <xsl:template match="/collection">
          <ul id="case-study-icons">
             <xsl:apply-templates select="content[(position() - 1) mod $itemsperlist = 0]" mode="first" />
          </ul>
       </xsl:template>
    
       <xsl:template match="content" mode="first">
          <li>
             <xsl:apply-templates select=".|following-sibling::content[position() &lt; $itemsperlist]" />
          </li>
       </xsl:template>
    
       <xsl:template match="content">
          <a href="{root/link}" title="{title}">
             <img src="{root/thumb/img/@src}" alt="{title}" width="92" height="46" />
          </a>
       </xsl:template>
    </xsl:stylesheet>
    
    
    


  • 请注意,如果您将3更改为1

    ,则此解决方案将起作用。我的当前XSLT已如上所述编写。目前,它每“li”输出1个图像链接。我想把它改为每“li”3个图像链接。XSL的输入是一个.NET对象列表。我指的是应用转换的XML输入。不幸的是,我没有这些信息。项目通过我无权访问的控件呈现:-(应用这个XSLT
    来查看原始XML。添加到顶部的问题中。:-)我有一个最新的XSLT,上面已经写过了。目前,它每“li”输出1个图像链接。我想把它改为每“li”3个图像链接。XSL的输入是一个.NET对象列表。我指的是应用转换的XML输入。不幸的是,我没有这些信息。项目通过我无权访问的控件呈现:-(应用此XSLT
    查看原始XML。添加到顶部的问题中。:-)感谢David,出于某种原因,上面的XSLT没有在页面上显示任何内容,这意味着上面的XSLT有问题。啊,如果第一行上的
    [
    ]
    一样多,这会有所帮助。将更新。除非您尚未更新,否则将保持不变。XML输入位于顶部(如果有帮助的话)。您的输入文件包含元素集合和内容,但您建议的xslt包含大写C的集合/内容。XML区分大小写,如果这是您的输入文件,则您的原始代码没有生成您指示的输出,因此建议的更改不会进行预期的更改。您需要发布一个示例输入,最好包含3个以上的项,并使用正确的元素名称。(或者只是更改xslt以匹配输入中的名称)上面的XML是从Firebug输出中复制的,并放在上面带有@Kirill Polishchuk建议的页面上。上面的XSLT就是代码中的内容,我只是从VisualStudio复制了它。因此,Firebug可能会将它们呈现为简单的字母。我已经将您的代码更改为简单的字母,但仍然没有luckThanks David,出于某种原因,上面没有显示任何内容,这意味着上面的XSLT有问题。啊,如果第一行的
    [
    数量与
    ]
    数量相同,这会有所帮助。将更新。除非您尚未更新,否则将保持不变。XML输入位于顶部(如果有帮助的话)。您的输入文件包含元素集合和内容,但您建议的xslt包含大写C的集合/内容。XML区分大小写,如果这是您的输入文件,则您的原始代码没有生成您指示的输出,因此建议的更改不会进行预期的更改。您需要发布一个示例输入,最好包含3个以上的项,并使用正确的元素名称。(或者只是更改xslt以匹配输入中的名称)上面的XML是从Firebug输出中复制的,并放在上面带有@Kirill Polishchuk建议的页面上。上面的XSLT就是代码中的内容,我只是从VisualStudio复制了它。因此,Firebug可能会将它们呈现为简单的字母。我把你的密码改成了简单的字母,但还是不走运