在XSLT中,如何更新循环中的多个元素 [英] In XSLT, how do you update multiple elements within a loop

查看:79
本文介绍了在XSLT中,如何更新循环中的多个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

首先,感谢您抽出时间查看我的问题。

我试图想出一个循环模板的代码,

找到一个名为start_time的元素并调用该元素的值,并减去因夏令时而导致的时间变化的小时。并且有多个开始时间,这就是为什么我将不得不使用循环。



这是原始的xml。

那里是PlaylistItem中存在的其他元素,但我已将它们删除为

simple。例如,所需的输出是



2015-10-12T11:07:39.924

将是

2015-10-12T12:07:39.924



在Block内的PlaylistItem中为每个人换回一个小时。

你的非常感谢帮助。



< Block> 
< PlaylistItem>
< StartTime> 2015-10-12T11:59:42.201< / StartTime>
< / PlaylistItem>
< PlaylistItem>
< StartTime> 2015-10-12T11:07:39.924< / StartTime>
< / PlaylistItem>
< PlaylistItem>
< StartTime> 2015-10-12T20:29:42.211< / StartTime>
< / PlaylistItem>
< PlaylistItem>
< StartTime> 2015-10-12T09:39:58.901< / StartTime>
< / PlaylistItem>
< PlaylistItem>
< StartTime> 2015-10-12T12:04:50.551< / StartTime>
< / PlaylistItem>
< / Block>





我对这个XSLT世界很新,我只有一个基本的逻辑来解决这个问题。所以我的逻辑是

1)遍历Block

2)访问PlaylistItem,访问StartTime

3)抓住这个字符串

4)使用子字符串并获取字符串中的小时字符

5)从中减去一个然后保存它

6)确保这适用阻止每个播放列表中的每个startTime。



我正在尝试为此搜索正确的语法然后实现它但是

目前我没有任何有效的可执行代码,我可以在这里发布。

请帮忙!当我尝试自己编写代码时,我将更新这篇文章。

再次感谢。

解决方案

XSLT本质上是递归的所以你不需要循环。



这个解决方案适合你:



 <   xsl:  style     版本  =  2.0    xmlns:xsl   =  http://www.w3.org/1999/XSL/转换 >  
xmlns:msxsl =urn:schemas-microsoft-com:xsltexclude-result-prefixes =msxsl >

< xsl:output 方法 = xml 缩进 = / >

< xsl:template < span class =code-attribute> match = 阻止 >
< xsl:element name = 阻止 >
< xsl:apply-templates 选择 = PlaylistItem / >
< span class =code-keyword>< / xsl:element >
< / xsl:template >

< xsl:template 匹配 = PlaylistItem >
< xsl:element 名称 = PlaylistItem >
< xsl:variable 名称 = 小时 >
select =substring(substring-after(StartTime,'T'),1,2)/>
< xsl:element 名称 = StartTime >
< xsl:choose >
< xsl:when test = < span class =code-keyword>


hour& lt; 23 >
< xsl:value-of

选择 = concat(sub string-before(StartTime,'T'),'T',


hour + 1,
substring(substring-after(StartTime,'T'),3)) / >
< / xsl:when >
< xsl:otherwise >
< xsl:value-of

选择 = concat(substring-before(StartTime, 'T'),'T',00,
substring(substring-after(StartTime,'T'),3))
/ >
< / xsl:否则 >
< / xsl:choose < span class =code-keyword>>
< / xsl:element >
< / xsl:template >
< / xsl: style sheet >





可能有更优雅的方式,但你可以自己找到。


Hello,
First, thanks for your time for having a look at my question.
I am trying to come up with a code that loops through a template,
finds an element called start_time and call the value of the element and subtract an hour for the time change due to daylight savings. And there are multiple start times and that's why I am going to have to use a loop.

This is the original xml.
There are other elements existing inside PlaylistItem, but I have removed them for
simplicity. The desired output is, for example,

2015-10-12T11:07:39.924
to be
2015-10-12T12:07:39.924

shifted back by an hour for every one of them inside PlaylistItem inside Block.
Your help will be very much appreciated.

<Block>
<PlaylistItem>
    <StartTime>2015-10-12T11:59:42.201</StartTime>
</PlaylistItem>
<PlaylistItem>
    <StartTime>2015-10-12T11:07:39.924</StartTime>
</PlaylistItem>
<PlaylistItem>
    <StartTime>2015-10-12T20:29:42.211</StartTime>
</PlaylistItem>
<PlaylistItem>
    <StartTime>2015-10-12T09:39:58.901</StartTime>
</PlaylistItem>
<PlaylistItem>
    <StartTime>2015-10-12T12:04:50.551</StartTime>
</PlaylistItem>
</Block>



I am very new to this XSLT world and I only have a basic piece of logic for this problem. So my logic here is to
1)iterate through the Block
2)access PlaylistItem, access StartTime
3)grab this string
4)use substring and grab the hour characters in the string
5)subtract one from it and then save it
6)make sure this applies to every startTime inside every Playlist in Block.

I'm trying to search for the proper syntax for this and then implement it but
right at the moment I don't have any valid executable code that I can post here.
Please help! And I'll be updating this post as I try to write the code myself.
Thanks again.

解决方案

XSLT is recursive by nature so you don't need a loop.

This solution should work for you:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    
    <xsl:output method="xml" indent="yes" />
    
    <xsl:template match="Block">
        <xsl:element name="Block">
            <xsl:apply-templates select="PlaylistItem" />
        </xsl:element>
    </xsl:template>
    
    <xsl:template match="PlaylistItem">
        <xsl:element name="PlaylistItem">
            <xsl:variable name="hour">
                select="substring(substring-after(StartTime, 'T'), 1, 2)" />
            <xsl:element name="StartTime">
                <xsl:choose>
                    <xsl:when test="


hour &lt; 23"> <xsl:value-of select="concat(substring-before(StartTime, 'T'), 'T',


hour+1, substring(substring-after(StartTime, 'T'), 3))" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="concat(substring-before(StartTime, 'T'), 'T', 00, substring(substring-after(StartTime, 'T'), 3))" /> </xsl:otherwise> </xsl:choose> </xsl:element> </xsl:template> </xsl:stylesheet>



There are probably more elegant ways of doing it, but that you can find out for yourself.


这篇关于在XSLT中,如何更新循环中的多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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