probleme au niveau de mes conditions

probleme au niveau de mes conditions - XML/XSL - Programmation

Marsh Posté le 13-12-2015 à 13:17:00    

bonjour  
je viens de me lancer depuis quelques jours dans le xsl pour créer un sheet pour mon lecteur multimedia...
En faisant des recherches a droite et a gauche je suis arrivé a faire ce que je vouloais. Voulant etre un peu plus detaillé, je bloque sur le code avec la fonction <xsl:when>
voici mon code:

Code :
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:myCPrefix="urn:custom-net"  
  3. xmlns:myJSPrefix="urn:custom-javascript"  exclude-result-prefixes="msxsl myCPrefix myJSPrefix">
  4.  
  5.     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  6.     
  7.     <xsl:variable name="folderflags" select="string('\..\Common\lstarflags\')"/>
  8.     <xsl:variable name="folderchannel" select="string('\..\Common\Channels\')"/>
  9.    <xsl:variable name="foldercodec" select="string('\..\Common\sound_format\')"/>    
  10.     <xsl:variable name="folderprofile" select="string('\..\Common\sound_format\')"/>
  11.    <xsl:variable name="pathvalue" select="//tokens/token[@name='%PATH%']"/>
  12.     <xsl:variable name="imageext" select="string('.png')"/>
  13.     
  14.     <xsl:variable name="audiolanguage1" select="//File/track[@type='Audio']/Language"/>
  15.     <xsl:variable name="audiolanguage2" select="//File/track[@type='Audio' and @streamid='2']/Language"/>
  16.     <xsl:variable name="audiolanguage3" select="//File/track[@type='Audio' and @streamid='3']/Language"/>
  17.     
  18.     <xsl:variable name="profileaudio1" select="//File/track[@type='Audio']/Format_profile"/>
  19.     <xsl:variable name="profileaudio2" select="//File/track[@type='Audio' and @streamid='2']/Format_profile"/>
  20.     
  21.     <xsl:variable name="audiocodec1" select="//File/track[@type='Audio']/Format"/>
  22.     <xsl:variable name="audiocodec2" select="//File/track[@type='Audio' and @streamid='2']/Format"/>
  23.     
  24.     
  25.     <xsl:variable name="audiochannels1" select="//File/track[@type='Audio']/Channel_s_"/>
  26.       <xsl:variable name="audiochannels2" select="//File/track[@type='Audio' and @streamid='2']/Channel_s_"/>
  27.      
  28.  
  29.     <xsl:variable name="flag1" select="concat($pathvalue,$folderflags,$audiolanguage1,$imageext)"/>
  30.     <xsl:variable name="flag2" select="concat($pathvalue,$folderflags,$audiolanguage2,$imageext)"/>
  31.     <xsl:variable name="flag3" select="concat($pathvalue,$folderflags,$audiolanguage3,$imageext)"/>
  32.     
  33.     <xsl:variable name="codec1" select="concat($pathvalue,$foldercodec,$audiocodec1,$imageext)"/>
  34.    <xsl:variable name="codec2" select="concat($pathvalue,$foldercodec,$audiocodec2,$imageext)"/>
  35.     <xsl:variable name="codec3" select="concat($pathvalue,$foldercodec,'HR.png')"/>
  36.     <xsl:variable name="codec4" select="concat($pathvalue,$foldercodec,'MA.png')"/>
  37.     
  38.     
  39.     <xsl:variable name="Channel1" select="concat($pathvalue,$folderchannel,$audiochannels1,$imageext)"/>
  40.     <xsl:variable name="Channel2" select="concat($pathvalue,$folderchannel,$audiochannels2,$imageext)"/>
  41.     
  42.  
  43.     <xsl:variable name="STUDIOS" select="string(//tokens/token[@name='%STUDIOS%'])"/>
  44.     <xsl:variable name="PATH" select="string(//tokens/token[@name='%PATH%'])"/>
  45.     <xsl:variable name="rating" select="//tokens/token[@name='%RATING%']"/>
  46.  
  47.     <msxsl:script implements-prefix="myJSPrefix" language="JavaScript">
  48.  
  49.  <![CDATA[
  50.  
  51.    function trim(oString)
  52.    {
  53.      return oString.replace(/^([\s\t\n]|\&nbsp\;)+|([\s\t\n]|\&nbsp\;)+$/g, '');
  54.    }
  55.  
  56.     var Studios = [];     //Stores the Studio Names
  57.  
  58.     function findStudios(oStudios, oTemplatePath)
  59.     {
  60.       Studios = oStudios.split("," );
  61.       var oFile = new ActiveXObject("Scripting.FileSystemObject" );
  62.       var path='';
  63.       for(var i = 0; i < Studios.length; i++)
  64.       {
  65.         path = oTemplatePath + '\\..\\common\\studios\\' + trim(Studios[i]) + '.png';
  66.         if (oFile.FileExists(path))
  67.         {
  68.           return trim(Studios[i]);
  69.         }
  70.       }
  71.       return trim(Studios[0]);
  72.     }
  73.     
  74.    ]]>
  75.  
  76.  </msxsl:script>
  77.  
  78.     <msxsl:script implements-prefix="myCPrefix" language="CSharp">
  79.  
  80.    <![CDATA[
  81.          public int CalculateRatingWidth(string rating, int totalWidth)
  82.          {
  83.            string[] _r = rating.Split('/');
  84.            if (_r.Length == 2)
  85.            {
  86.                double _rating = !string.IsNullOrEmpty(_r[0]) ? Convert.ToDouble(_r[0],System.Globalization.CultureInfo.InvariantCulture) : 1;
  87.                return (int)(totalWidth * _rating / 10);
  88.            }
  89.            return 1;
  90.          }
  91.    ]]>
  92. </msxsl:script>
  93.  
  94.  
  95.     <xsl:template match="@*|node()">
  96.         <xsl:copy>
  97.             <xsl:apply-templates select="@*|node()"/>
  98.         </xsl:copy>
  99.     </xsl:template>
  100.  
  101.     <xsl:variable name="RatingWidth" select="//Elements/ImageElement[@Name='Bars']/@Width"/>
  102.     <xsl:variable name="CalculatedRatingWidth" select="myCPrefix:CalculateRatingWidth(string($rating), $RatingWidth)"/>
  103.     <xsl:template match="//Elements/ImageElement[@Name='Bars']/Actions/Crop/@Width">
  104.         <xsl:attribute name="Width"><xsl:value-of select="$CalculatedRatingWidth"/></xsl:attribute>
  105.     </xsl:template>
  106.  
  107.     <xsl:template match="//ImageElement[@Name='Studio']/@SourceData">
  108.         <xsl:attribute name="SourceData">
  109.           <xsl:value-of select="concat($PATH,'\..\common\Studios\',string(myJSPrefix:findStudios($STUDIOS,$PATH)),'.png')"/>
  110.         </xsl:attribute>
  111.     </xsl:template>
  112.  
  113.     <xsl:template match="@*|node()">
  114.         <xsl:copy>
  115.             <xsl:apply-templates select="@*|node()"/>
  116.         </xsl:copy>
  117.     </xsl:template>
  118.     <xsl:template match="ImageElement[@Name='Aud1']">
  119.         <xsl:copy>
  120.             <xsl:apply-templates select="@*|node()"/>
  121.             <xsl:choose>
  122.                 <xsl:when test="$audiolanguage1 != '' ">
  123.                     <xsl:attribute name="SourceData"><xsl:value-of select="$flag1"/></xsl:attribute>
  124.                 </xsl:when>
  125.             </xsl:choose>
  126.         </xsl:copy>
  127.     </xsl:template>
  128.     <xsl:template match="ImageElement[@Name='Aud2']">
  129.         <xsl:copy>
  130.             <xsl:apply-templates select="@*|node()"/>
  131.             <xsl:choose>
  132.                 <xsl:when test="$audiolanguage2 != '' ">
  133.                     <xsl:attribute name="SourceData"><xsl:value-of select="$flag2"/></xsl:attribute>
  134.                 </xsl:when>
  135.             </xsl:choose>
  136.         </xsl:copy>
  137.     </xsl:template>
  138.     <xsl:template match="ImageElement[@Name='Aud3']">
  139.         <xsl:copy>
  140.             <xsl:apply-templates select="@*|node()"/>
  141.             <xsl:choose>
  142.                 <xsl:when test="$audiolanguage3 != '' ">
  143.                     <xsl:attribute name="SourceData"><xsl:value-of select="$flag3"/></xsl:attribute>
  144.                 </xsl:when>
  145.             </xsl:choose>
  146.         </xsl:copy>
  147.     </xsl:template>
  148.     
  149.     
  150.     <xsl:template match="@*|node()">
  151.         <xsl:copy>
  152.             <xsl:apply-templates select="@*|node()"/>
  153.         </xsl:copy>
  154.     </xsl:template>
  155.     <xsl:template match="ImageElement[@Name='Codecaudio1']">
  156.         <xsl:copy>
  157.             <xsl:apply-templates select="@*|node()"/>
  158.             <xsl:choose>
  159.                 <xsl:when test ="$profileaudio1 !='HRA_/_Core'">
  160.                     <xsl:attribute name="SourceData"><xsl:value-of select="$codec3"/></xsl:attribute>
  161.                 </xsl:when>
  162.                 <xsl:when test ="$profileaudio1 != 'MA_/_Core' ">
  163.                     <xsl:attribute name="SourceData"><xsl:value-of select="$codec4"/></xsl:attribute>
  164.                 </xsl:when>
  165.                 <xsl:otherwise >
  166.                     <xsl:attribute name="SourceData"><xsl:value-of select="$codec1"/></xsl:attribute>
  167.                  </xsl:otherwise>
  168.             </xsl:choose>
  169.         </xsl:copy>
  170.     </xsl:template>
  171.     <xsl:template match="@*|node()">
  172.         <xsl:copy>
  173.             <xsl:apply-templates select="@*|node()"/>
  174.         </xsl:copy>
  175.     </xsl:template>
  176.     <xsl:template match="ImageElement[@Name='Codecaudio2']">
  177.         <xsl:copy>
  178.             <xsl:apply-templates select="@*|node()"/>
  179.             <xsl:choose>
  180.                 <xsl:when test="$audiocodec2 != 'HRA_/_Core' ">
  181.                     <xsl:attribute name="SourceData"><xsl:value-of select="$codec3"/></xsl:attribute>
  182.                 </xsl:when>
  183.                 <xsl:when test="$audiocodec2 != 'MA_/_Core' ">
  184.                     <xsl:attribute name="SourceData"><xsl:value-of select="$codec4"/></xsl:attribute>
  185.                 </xsl:when>
  186.                 <xsl:when test="$audiocodec2 != '' ">
  187.                     <xsl:attribute name="SourceData"><xsl:value-of select="$codec1"/></xsl:attribute>
  188.                 </xsl:when>
  189.             </xsl:choose>
  190.         </xsl:copy>
  191.     </xsl:template>
  192.     
  193.     
  194.     <xsl:template match="ImageElement[@Name='chan1']">
  195.         <xsl:copy>
  196.             <xsl:apply-templates select="@*|node()"/>
  197.             <xsl:choose>
  198.                 <xsl:when test="$audiochannels1 != '' ">
  199.                     <xsl:attribute name="SourceData"><xsl:value-of select="$Channel1"/></xsl:attribute>
  200.                 </xsl:when>
  201.             </xsl:choose>
  202.         </xsl:copy>
  203.     </xsl:template>
  204.     <xsl:template match="ImageElement[@Name='chan2']">
  205.         <xsl:copy>
  206.             <xsl:apply-templates select="@*|node()"/>
  207.             <xsl:choose>
  208.                 <xsl:when test="$audiochannels2 != '' ">
  209.                     <xsl:attribute name="SourceData"><xsl:value-of select="$Channel2"/></xsl:attribute>
  210.                 </xsl:when>
  211.             </xsl:choose>
  212.         </xsl:copy>
  213.     </xsl:template>
  214.     
  215.     
  216.     
  217. </xsl:stylesheet>


la ou je peche, c'est apartir de la ligne 158, en effet, a partir du moment ou la ligne "FORMAT PROFILE" existe sur mon fichier source, il affiche systématiquement l'image correspondante a la valeur "HRA / Core"
Mais moi ce que je veux c'est qu'il ne prenne en compte la ligne "format profile" que si elle a la valeur "HRA /Core" pour laquelle elle affiche l'image correspondante ou si la valeur est " MA / Core" la aussi elle renverra l'image correspondante;
Si il n'ya pas une de ces deux valeur, alors mettre l'image correspondante au  codec1
Mais je n'y arrive pas  a chaque fois que la ligne "FORMAT profile " existe il me renvoie l'image du HRA / Core quelque soit la valeur de format profile
 
pourriez vous m'aider svp ca fait deux jours que je suis dessus et je n'y arrive  pas (dur dur pour un debutant)
merci par avance  
cordialement


Message édité par gilou le 13-12-2015 à 17:39:53
Reply

Marsh Posté le 13-12-2015 à 13:17:00   

Reply

Marsh Posté le 16-12-2015 à 14:56:46    

Bonjour,
 
Attention, dans ton test ligne 159 ci-dessous tu vérifie profileaudio1 DIFFERENT de HRA_/_Core
 

Code :
  1. $profileaudio1 !='HRA_/_Core'


 
En effet, l'opérateur != signifie différent en XSL. Essaye tout simplement en faisant le test d'égalité plutôt...

Reply

Marsh Posté le 18-12-2015 à 19:08:00    

Bonjour chelmi,
merci pour cette reponse cela m'a beaucoup aidé...
Maintenant j'ai un autre probleme voulant alléger le truc je cherche le moyen d'assembler deux resultat pour rechercher une image correspondante..
Je m'explique admettons qu'il me trouve le  codec3  (ligne 155) et que ce soit "ac-3", et que le channels1 (ligne 198) correspond a "6 channels"..., a la place de me mettre 2 images, une correspondante au AC-3 et l'autre au 6 channels, j'aimerais qu'il me fasse apparaitre une image qui serait nommé "ac-3 6 chanels"
 
J'ai essayé avec cette phrase:  <xsl:attribute name="SourceData"><xsl:value-of select="$audiocodec3 and $audiochannels1"/></xsl:attribute>
 
mais vu mon niveau cela ne fonctionne pas bien evidemment.. auriez de quoi m'aiguiller svp
merci par avance

Reply

Marsh Posté le 21-12-2015 à 13:32:38    

Dans ce cas, ce n'est pas le SourceData qu'il faut modifier mais le test en lui-même pour passer par des conditions multiples.
 
Quelque chose comme :
 

Code :
  1. <xsl:when test ="$audiocodec3='ac-3' and $audiochannels1='6 channels'">
  2.       <xsl:attribute name="SourceData"><xsl:value-of select="$codec36"/></xsl:attribute>
  3. </xsl:when>


 
où $codec36 correspond à l'image que tu veux mettre.
 
Je pense que tu mélanges un peu les notions et la logique du XSL ...  
 
Pour schématiser, dans les lignes ci-dessous  
 

Code :
  1. <xsl:template match="ImageElement[@Name='Aud1']">
  2.         <xsl:copy>
  3.             <xsl:apply-templates select="@*|node()"/>
  4.             <xsl:choose>
  5.                 <xsl:when test="$audiolanguage1 != '' ">
  6.                     <xsl:attribute name="SourceData"><xsl:value-of select="$flag1"/></xsl:attribute>
  7.                 </xsl:when>
  8.             </xsl:choose>
  9.         </xsl:copy>
  10.     </xsl:template>


 
la ligne 1 indique où tu vas afficher ton image
la ligne 3 indique quel élément tu vas tester (ici tu vérifies dans tous les "noeuds" de ton fichiers)
la ligne 5 est le test qui vérifie la condition (le noeud $audiolanguage n'est pas vide)
la ligne 6 est l'attribution de la valeur à l'élément image si le test est OK ("ImageElement[@Name='Aud1']" de la ligne 1 vaudra "$flag1" )
 
Donc, si tu veux modifier les conditions d'affichage de ton image, c'est bien le test (ici en ligne 5) qu'il faut modifier.
 
Bon courage.
 :D

Reply

Marsh Posté le 26-12-2015 à 22:07:35    

bonjour chelmi18,
merci pour cette réponse, mais effectivement si je dois refaire cette manip pour chaque image et bien j'ai pas fini, je pensais qu'il etait possible de recuperer la valeur des 2 sources data et en faisant un truc du style  " valeur source data 1" et "valeur source data 2" donne image 1 par exemple ..
Mais s'il faut definir tous les noms sois meme ca va etre trop lourd..avec toutes les possibilités...
Bon je crois que je vais oublier ca...

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed