对 XSLT 变量日期进行排序

发布于 2024-10-26 02:40:54 字数 3477 浏览 1 评论 0原文

抱歉,如果我在上一个“问题”中的解释有点粗糙,我会再试一次。我对 xsl 真的很陌生。

通过一系列子字符串,我通过将日期字段存储在名为 $releasedate 的变量中来提取日期字段,我使用 它将在“foreach”循环中显示 dd/mm/yyyy,例如 29/03/2010,如下所示。

我的 xml 结构类似于下面,

<item>
<title>Title Goes Here</title>
<description>test goes here.....<b>Release Date:22/20/2010</b> more text</description>
</item>
<item>
<title>Title Goes Here</title>
<description>More test goes here.....<b>Release Date:22/20/2010</b> more text</description>
</item>

我希望能够按 $releasedate 中存储的值进行排序。 我想也许它看起来像下面这样,

<xsl:sort select="$releasedate" order="descending" />

我希望这更有意义,我再次为我缺乏这方面的知识而道歉。

下面是我的xsl结构

<xsl:for-each select="item">
    <xsl:sort select="pubDate"/>
    <xsl:if test="position() &lt; 5 ">
        <!-- grabs items 1 to 5 -->
        <xsl:variable name = "releasedatestart" >
            <xsl:value-of select="substring-after(description,'Release Date:&lt;/b&gt;')"/>
        </xsl:variable>
        <xsl:variable name = "releasedate" >
            <xsl:value-of select="substring-before($releasedatestart,'&lt;/div&gt;')"/>
        </xsl:variable>
        <xsl:variable name = "displaynamestart" >
            <xsl:value-of select="substring-after(description,'Display Name:&lt;/b&gt;')"/>
        </xsl:variable>
        <xsl:variable name = "displayname" >
            <xsl:value-of select="substring-before($displaynamestart,'&lt;/div&gt;')"/>
        </xsl:variable>
        <div style="margin:0px;background-color:#f2eff3;border:1px solid #ded6df;">
            <xsl:variable name = "start" >
                <xsl:value-of select="substring-after(description,'Description:&lt;/b&gt;')"/>
            </xsl:variable>
            <xsl:variable name = "title" >
                <xsl:value-of select="substring($start,0,100)"/>
            </xsl:variable>
            <xsl:variable name = "pagelink" >
                <xsl:value-of select="substring(link,1,61)"/>
            </xsl:variable>
            <xsl:variable name = "pageurl" >
                <xsl:value-of select="$pagelink"/>
                <xsl:value-of select="title"/>.aspx
            </xsl:variable>
            <div style="min-height:50px;">
                <div class="column" id="feeddate">
                    <xsl:value-of select="$releasedate" />
                </div>
                <div class="column" id="displayname">
                    <xsl:value-of select="$displayname" />
                </div>
                <div class="column" id="feedtitle">
                    <xsl:value-of select="$title" disable-output-escaping="yes"/>
                    <a>
                        <xsl:attribute name="href">
                            <xsl:value-of select="$pageurl" />
                        </xsl:attribute>
                        ..Read More
                    </a>
                </div>
            </div>
        </div>
    </xsl:if>
</xsl:for-each>

Sorry if my explanation in the last 'question' was a little rough, I'll give it another shot.I'm really new to xsl.

Through a series of substrings I have extracted the date field by storing it in a variable called $releasedate, I output this by using <xsl:value-of select="$releasedate" /> which would display dd/mm/yyyy e.g. 29/03/2010 in a 'for each' loop as shown below.

My xml is structured similar to below,

<item>
<title>Title Goes Here</title>
<description>test goes here.....<b>Release Date:22/20/2010</b> more text</description>
</item>
<item>
<title>Title Goes Here</title>
<description>More test goes here.....<b>Release Date:22/20/2010</b> more text</description>
</item>

I would like to be able to sort by the value of what is stored in $releasedate.
I think maybe it would look like this below

<xsl:sort select="$releasedate" order="descending" />

I hope this makes a little more sense, my appologies again for my lack of knowledge in this.

Below is my xsl structure

<xsl:for-each select="item">
    <xsl:sort select="pubDate"/>
    <xsl:if test="position() < 5 ">
        <!-- grabs items 1 to 5 -->
        <xsl:variable name = "releasedatestart" >
            <xsl:value-of select="substring-after(description,'Release Date:</b>')"/>
        </xsl:variable>
        <xsl:variable name = "releasedate" >
            <xsl:value-of select="substring-before($releasedatestart,'</div>')"/>
        </xsl:variable>
        <xsl:variable name = "displaynamestart" >
            <xsl:value-of select="substring-after(description,'Display Name:</b>')"/>
        </xsl:variable>
        <xsl:variable name = "displayname" >
            <xsl:value-of select="substring-before($displaynamestart,'</div>')"/>
        </xsl:variable>
        <div style="margin:0px;background-color:#f2eff3;border:1px solid #ded6df;">
            <xsl:variable name = "start" >
                <xsl:value-of select="substring-after(description,'Description:</b>')"/>
            </xsl:variable>
            <xsl:variable name = "title" >
                <xsl:value-of select="substring($start,0,100)"/>
            </xsl:variable>
            <xsl:variable name = "pagelink" >
                <xsl:value-of select="substring(link,1,61)"/>
            </xsl:variable>
            <xsl:variable name = "pageurl" >
                <xsl:value-of select="$pagelink"/>
                <xsl:value-of select="title"/>.aspx
            </xsl:variable>
            <div style="min-height:50px;">
                <div class="column" id="feeddate">
                    <xsl:value-of select="$releasedate" />
                </div>
                <div class="column" id="displayname">
                    <xsl:value-of select="$displayname" />
                </div>
                <div class="column" id="feedtitle">
                    <xsl:value-of select="$title" disable-output-escaping="yes"/>
                    <a>
                        <xsl:attribute name="href">
                            <xsl:value-of select="$pageurl" />
                        </xsl:attribute>
                        ..Read More
                    </a>
                </div>
            </div>
        </div>
    </xsl:if>
</xsl:for-each>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜警司 2024-11-02 02:40:54

此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="items">
  <items>
    <xsl:apply-templates>
      <xsl:sort select=
      "substring(substring-after(description, 'Release Date:'),7)"
       data-type="number" order="descending"/>
      <xsl:sort select=
      "substring(substring-after(description, 'Release Date:'),4,2)"
       data-type="number" order="descending"/>
      <xsl:sort select=
      "substring(substring-after(description, 'Release Date:'),1,2)"
       data-type="number" order="descending"/>
     </xsl:apply-templates>
  </items>
 </xsl:template>
</xsl:stylesheet>

应用于此文档时

<items>
    <item>
        <title>Title1</title>
        <description>text1 .....
            <b>Release Date:22/12/2010</b> more text
        </description>
    </item>
    <item>
        <title>Title Goes Here</title>
        <description>More test goes here.....
            <b>Release Date:23/12/2010</b> more text
        </description>
    </item>
</items>

产生所需的排序结果

<items>
   <item>
      <title>Title Goes Here</title>
      <description>More test goes here.....
            <b>Release Date:23/12/2010</b> more text
        </description>
   </item>
   <item>
      <title>Title1</title>
      <description>text1 .....
            <b>Release Date:22/12/2010</b> more text
        </description>
   </item>
</items>

说明

  1. 使用substring-after()substring()

  2. 的多个 子级。

  3. 身份转换。

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="items">
  <items>
    <xsl:apply-templates>
      <xsl:sort select=
      "substring(substring-after(description, 'Release Date:'),7)"
       data-type="number" order="descending"/>
      <xsl:sort select=
      "substring(substring-after(description, 'Release Date:'),4,2)"
       data-type="number" order="descending"/>
      <xsl:sort select=
      "substring(substring-after(description, 'Release Date:'),1,2)"
       data-type="number" order="descending"/>
     </xsl:apply-templates>
  </items>
 </xsl:template>
</xsl:stylesheet>

when applied on this document:

<items>
    <item>
        <title>Title1</title>
        <description>text1 .....
            <b>Release Date:22/12/2010</b> more text
        </description>
    </item>
    <item>
        <title>Title Goes Here</title>
        <description>More test goes here.....
            <b>Release Date:23/12/2010</b> more text
        </description>
    </item>
</items>

produces the wanted, sorted result:

<items>
   <item>
      <title>Title Goes Here</title>
      <description>More test goes here.....
            <b>Release Date:23/12/2010</b> more text
        </description>
   </item>
   <item>
      <title>Title1</title>
      <description>text1 .....
            <b>Release Date:22/12/2010</b> more text
        </description>
   </item>
</items>

Explanation:

  1. Use of substring-after() and substring().

  2. Multiple <xsl:sort> children of <xsl:apply-templates>.

  3. Identity transform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文