嵌套循环对于 xslt 无法正常工作

发布于 2024-09-18 12:40:07 字数 2613 浏览 8 评论 0原文

我一直在使用名为 Umbraco(umbraco.org) 的 CMS,并使用 SlideShowPro 独立产品(www.slideshowpro.net) 在网站上显示 Flash 新闻。

简而言之,我在管理面板上创建了一个部分,如下所示。

-Flash(有一个xslt文件)
- 月份名称
- 带有图像的节点
- 带有视频的节点

每个月我都会创建一个带有月份名称的节点并向其中添加图像和视频。月份节点可能包含所有图像或视频,或者两者都是混合的。 我没有任何输入 xml 文件,因为 SlideShowPro Standalone 上只有一个文件,可以输出 flash 所需的 xml 文件,

这里是 xml 文件:

<album id="ssp" lgPath="" tnPath="" title="Album One" description="" tn="">
    <img src="1.jpg" id="id1" title="" caption="" link="" target="_blank" pause="" />
    <img src="1.f4v" id="id1" tn="" title="" caption="" link="" target="_blank" pause="" vidpreview" />
</album>

我在 xslt 文件中所做的是;

<xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']">
            <album lgPath="http://localhost" tnPath="http://localhost" title="{data[@alias = 'albumTitle']}" description="{data[@alias = 'albumDescription']}" tn="http://localhost">
                <xsl:for-each select="node">
                    <xsl:if test = "string-length(./data [@alias = 'image']) &gt; 0" >                                                  
                                                <img src="{data[@alias = 'image']}" title="{data[@alias = 'title']}" caption="{data[@alias = 'caption']}" link="{data[@alias = 'link']}" target="_blank" pause=""/>                                                         
                    </xsl:if>       
                    <xsl:if test = "string-length(./data [@alias = 'video']) &gt; 0" >                                                  
                                                <img src="{data[@alias = 'video']}" tn="http://localhost" title="{data[@alias = 'title']}" caption="{data[@alias = 'caption']}" link="{data[@alias = 'link']}" target="_blank" pause="" vidpreview="/flash/gallery/album2/video/1_preview.png"/>                                                        
                    </xsl:if>

                </xsl:for-each>     
                        </album>
        </xsl:for-each>

......

<album lgPath="http://localhost" tnPath="http://localhost" title="" description="" tn="http://localhost"><img src="/media/951/untitled.png" title="örnek" caption="örnek" link="" target="_blank" pause=""/><img src="/media/1026/1.f4v" title="flash" caption="flash" link="" target="_blank" pause=""/></album>

尽管拉什指出我应该使用陈述,但结果是相同的

I have been using a CMS which is called Umbraco(umbraco.org) and for displaying flash news on the website using SlideShowPro Standalone product(www.slideshowpro.net)

In brief I created a section on admin panel as follows.

-Flash(which has a xslt file)
- Month name
- A node with image
- A node with video

Every month I will be creating a node with name of the month and add image and videos to them. Month node might have all image or video perhaps both are mixed.
I do not have any input xml file cause on SlideShowPro Standalone has only a file which outputs desired xml file for flash

here is the xml file:

<album id="ssp" lgPath="" tnPath="" title="Album One" description="" tn="">
    <img src="1.jpg" id="id1" title="" caption="" link="" target="_blank" pause="" />
    <img src="1.f4v" id="id1" tn="" title="" caption="" link="" target="_blank" pause="" vidpreview" />
</album>

What I did in xslt file is;

<xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']">
            <album lgPath="http://localhost" tnPath="http://localhost" title="{data[@alias = 'albumTitle']}" description="{data[@alias = 'albumDescription']}" tn="http://localhost">
                <xsl:for-each select="node">
                    <xsl:if test = "string-length(./data [@alias = 'image']) > 0" >                                                  
                                                <img src="{data[@alias = 'image']}" title="{data[@alias = 'title']}" caption="{data[@alias = 'caption']}" link="{data[@alias = 'link']}" target="_blank" pause=""/>                                                         
                    </xsl:if>       
                    <xsl:if test = "string-length(./data [@alias = 'video']) > 0" >                                                  
                                                <img src="{data[@alias = 'video']}" tn="http://localhost" title="{data[@alias = 'title']}" caption="{data[@alias = 'caption']}" link="{data[@alias = 'link']}" target="_blank" pause="" vidpreview="/flash/gallery/album2/video/1_preview.png"/>                                                        
                    </xsl:if>

                </xsl:for-each>     
                        </album>
        </xsl:for-each>

and that outputs

<album lgPath="http://localhost" tnPath="http://localhost" title="" description="" tn="http://localhost"><img src="/media/951/untitled.png" title="örnek" caption="örnek" link="" target="_blank" pause=""/><img src="/media/1026/1.f4v" title="flash" caption="flash" link="" target="_blank" pause=""/></album>

Even though Larsh pointed out that I should use statament, the result is same...

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

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

发布评论

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

评论(3

知足的幸福 2024-09-25 12:40:07

我不确定您的源数据是什么样的,但假设它没有嵌套的;元素,问题是第二个 xsl:for-each 应该是

<xsl:for-each select=".">

因为第一个 for-each 内的上下文项将是。元素,因此您发布的指令将寻找另一个子元素。

I'm unsure what your source data is like, but assuming that it doesn't have nested <node> elements, the problem is that the second xsl:for-each should be

<xsl:for-each select=".">

because the context item inside the first for-each will be the <node> element, therefore the instruction you posted would be seeking a further child element.

殊姿 2024-09-25 12:40:07

纳西尔西尼兹?!

Deniz,如果能看到您输入的 XML,那就太好了。看来您在评论中发布的内容是一些所需的输出 XML。发布它的最佳方法是编辑原始问题并将输入 XML(至少是一个示例)放在那里。

同时,我想知道 元素是否可以同时拥有

<data alias="image">

子元素

<data alias="video">

子元素?如果是这样,那就可以解释为什么在这些情况下您没有获得 输出。

指令每次 < 时仅“执行” 指令之一/code> 被处理(在本例中, 循环的每次迭代处理一次)。因此,如果存在“data[@alias如果您需要能够处理图像数据子项和视频数据子项,请

<xsl:if test=...>

分别使用它们,而不是

<xsl:choose>
  <xsl:when test=...>

如果这不能解决问题,请发布示例输入 XML,并使用“代码”格式化按钮(带有 101 010 图标)对其进行格式化。

Nasılsınız?!

Deniz, it would still be good to see your input XML. It looks like what you posted in your comment was a bit of the desired output XML. The best way to post it is to edit your original question and put the input XML there, at least a sample.

In the meantime, I'm wondering, is it possible for a <node> element to have both a

<data alias="image">

child and a

<data alias="video">

child? If so, that would explain why in those cases you're not getting the <img src="{data[@alias = 'video']}"> output.

The <xsl:choose> instruction will only "execute" one of the <xsl:when> instructions each time the <xsl:choose> is processed (in this case, once for every iteration of the <xsl:for-each> loop). So if there is a `data[@aliasIf you need to be able to handle both an image data child and a video data child, use

<xsl:if test=...>

for each of them, instead of

<xsl:choose>
  <xsl:when test=...>

If that doesn't address the problem, please do post a sample of your input XML, and format it using the 'code' formatting button (with the 101 010 icon).

夏花。依旧 2024-09-25 12:40:07

更新:@deniz_seaside,请给我们 XML 文档,并让我们第一次看到它。

然后,您可能会再次编写您的转换,这次要好得多。

以下是如何获取您没有的 XML 文档:只需将此转换应用于这个未知文档即可:

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

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

或者更短

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

 <xsl:template match="/">
  <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>

<xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']"> 
            <album lgPath="http://localhost" tnPath="http://localhost" title="{data[@alias = 'albumTitle']}" description="{data[@alias = 'albumDescription']}" tn="http://localhost"> 
                <xsl:for-each select="node"> 

第二个 很可能是不正确的。它会选择处理当前节点中所有名为node的节点,该节点也名为node。通常,名为 node 的节点也没有名为 node 的子节点。

解决方案是删除第二个 指令,并将其内容(主体)保留在第一个 的主体中> 指导。

请注意

  1. 由于您没有提供任何源XML文档,您的问题很可能是由于恶劣天气造成的

  2. 切勿将名称节点用于元素、属性或处理指令。您感到困惑和犯错误的机会增加了 1000 倍。

UPDATE: @deniz_seaside, please give us the XML document and also see it for the first time.

Then, probably, you'll write your transformation again, this time much better.

Here is how to obtain the XML document that you don't have: just apply this transformation to this unknown document:

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

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

Or even shorter:

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

 <xsl:template match="/">
  <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>

<xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']"> 
            <album lgPath="http://localhost" tnPath="http://localhost" title="{data[@alias = 'albumTitle']}" description="{data[@alias = 'albumDescription']}" tn="http://localhost"> 
                <xsl:for-each select="node"> 

The second <xsl:for-each> is most likely incorrect. It will select for processing all nodes named node of the current node, which is also named node. Usually a node named node doesn't have children named node, too.

The solution is to remove the second <xsl:for-each> instruction and leave its content ( body) in the body of the first <xsl:for-each> instruction.

Do note:

  1. As you haven't provided any source XML document, your problem is most likely duew to the bad weather.

  2. Never use the name node for an element, attribute or processing-instruction. You are increasing the chances of getting confused and committing errors by a factor of 1000.

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