XSL does not enter the correct condition

Asked

Viewed 520 times

2

I have the following condition:

<xsl:when test="$Rows[1]/@Style='Resultado-Lista'">
 <ul>
  <xsl:for-each select="$Rows">
     <li>
        <a href="/{@FileRef}" title="{$Data} - {@Title}">
           <xsl:choose>
             <xsl:when test="(@PublishingRollupImage = '')">
               <img src="/Util/Imagens/Conteudo/sem-imagem-noticia.jpg" />
             </xsl:when>
             <xsl:otherwise>
               <xsl:value-of select="@PublishingRollupImage"  />
             </xsl:otherwise>
           </xsl:choose>
        </a>
     </li>
  </xsl:for-each>
 </ul>
</xsl:when>

In one of the results the field @PublishingRollupImage is fulfilled, but always enters the first condition.

I use a list of Sharepoint pages to search and the field is like "Publishing Image"

I’ve tried to do with xsl:if however, I believe I’m making the condition wrong. Someone can help me ?

  • There are several factors that can cause an element not to be selected, among them the context and namespaces. Please edit your query to include the source XML (or a fragment, including the hierarchy and root elements) and also an entire template of your XSL (if possible the entire XSLT doc, or at least the relevant start and templates).

  • I exemplified it better. I hope it helps. I removed only a few variable calls that do not influence the final result

  • The ideal would be to see the XML code. And more of XSLT that shows what is the contents of the variable $Rows since the attribute you want is in that context. I can try to reproduce the problem if you include more information (maybe all of XSLT and a snippet of XML-source that contains the data that is not appearing)

  • Unfortunately I can not inform you the full XML as it is Sharepoint and I do not have access to all the content. XSLT is very extensive and there is no need to add it here, just the call of the style I am assigning in the library query, which in this case is the "Result-List"

  • What is the value of the variable $Rows? What is the context of your template? What’s on match his?

  • The problem is not $Rows, as there are other values within this <xsl:for-each> and the data is returned correctly. Maybe the problem is in the validation of the "Publication Image" column, but unfortunately, I haven’t found what it might be causing. I also exported Webpart and added in the Commonviewfields field the @Publishingrollupimage column (if in doubt) and even then it is not displayed

Show 1 more comment

2 answers

1

Instead of comparing, you are assigning the value '' to the variable PublishingRollupImage. To check if it is filled, just do so:

<xsl:when test="@PublishingRollupImage">
  • It didn’t work. I already tried to play the <xsl:value-of select="@PublishingRollupImage" /> outside the <xsl:choose> and returns nothing

  • @Luannaiozzi this is evidence that so the problem is not in the query itself, but in the value stored in PublishingRollupImage. What is stored in that field?

  • I added an image, consulting the image library of this Subsite and selecting the desired one. The column is as Optional in SP

1

It is difficult to offer a solution in XSLT without knowing the structure of the input data, and without knowing exactly the context in which the processing takes place, so this response is based on several assumptions.

I did some research on Sharepoint and found that attribute PublishingRollupImage can contain (or always contains) embedded XML as raw text. This will make no difference when testing, but will have to be considered when displaying. You will have to print the result using disable-output-escaping if you want to generate XML.

As for the test you did, @Renan is right: the test test="@PublishingRollupImage = ''" test whether an attribute PublishingRollupImage exists and contains the empty string, while simply using test="@PublishingRollupImage" test whether the attribute exists. Are different things.

I created this test XML, which may be similar to what you are getting in that there are three elements <Row>. One that contains an attribute PublishingRollupImage containing embedded XML, another that contains the attribute with an empty string, and finally one that nay contains the attribute:

<root>
    <Row ID="1" Style="Resultado-Lista"
        Title="Imagem 1"
        PublishingRollupImage="&lt;img alt=&quot;&quot; src=&quot;/PublishingImages/imagem.jpg&quot; style=&quot;border:px solid&quot; /&gt;" />
    <Row ID="2" Style="Resultado-Lista"
        Title="Imagem Faltando"
        PublishingRollupImage="" />
    <Row ID="3" Style="Resultado-Lista"
        Title="Não é imagem" />
</root>

I processed this source document in the style sheet below (which included the three Row in the variable Rows):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes"/>
    <xsl:variable name="Rows" select="//Row"/>
    <xsl:template match="/">
       <xsl:choose>
           <xsl:when test="$Rows[1]/@Style='Resultado-Lista'">
               <ul>
                   <xsl:for-each select="$Rows">
                       <li>
                           <a href="/{@FileRef}" title="2014-05-26 - {@Title}">
                               <xsl:choose>
                                   <xsl:when test="(@PublishingRollupImage = '')">
                                       <img src="/Util/Imagens/Conteudo/sem-imagem-noticia.jpg" />
                                   </xsl:when>
                                   <xsl:otherwise>
                                       <xsl:value-of select="@PublishingRollupImage" disable-output-escaping="yes"  />
                                   </xsl:otherwise>
                               </xsl:choose>
                           </a>
                       </li>
                   </xsl:for-each>
               </ul>
           </xsl:when>
       </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

And the result was:

<ul>
   <li>
      <a href="/" title="2014-05-26 - Imagem 1"><img alt="" src="/PublishingImages/imagem.jpg" style="border:px solid" /></a>
   </li>
   <li>
      <a href="/" title="2014-05-26 - Imagem Faltando">
         <img src="/Util/Imagens/Conteudo/sem-imagem-noticia.jpg"/>
      </a>
   </li>
   <li>
      <a href="/" title="2014-05-26 - Não é imagem"/>
   </li>
</ul>

I put a XSLT Fiddle here with the example code above, which you can test in real time.

See if this helps to find the solution to your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.