开发者

Nested loops does not work correctly for xslt

开发者 https://www.devze.com 2023-01-14 22:07 出处:网络
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)

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:i开发者_Go百科f 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>

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


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.


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消