2017-10-18 6 views
1

J'utilise une feuille de style XSL pour transformer un document XML en HTML dans un environnement android.XSL utilisant le paramètre pour chaque

<?xml version="1.0" encoding="UTF-8"?> 
<list> 
    <project id="Proj02"> 
    <observation id="Proj02_Obs001"> 
    … 
    </observation> 
    </project> 
    <project id="Proj01"> 
    <observation id="Proj01_Obs002"> 
    … 
    </observation> 
    <observation id="Proj01_Obs001"> 
     … 
    </observation> 
    </project> 
</list> 

Pour mon fichier html, je veux parcourir toutes les observations d'un projet spécifique. Pour ce faire, je passe un paramètre pour le projet @ id:

transformer.setParameter("projID", "Proj01") 

Utilisation du fichier xsl

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
    <xsl:template match="/"> 
     <xsl:param name="projID" /> 
     <html> 
      <body> 
       <table> 
        <tr> 
         <th style="text-align:left">ID</th> 
         … 
        </tr> 
        <xsl:for-each select="list/project[@id=$projID]/observation"> 
         <tr> 
          <td> 
           <xsl:value-of select="@id" /> 
          </td> 
          … 
         </tr> 
        </xsl:for-each> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

Je reçois aucun résultat.

Comment dois-je modifier cette ligne?

<xsl:for-each select="list/project[@id=$projID]/observation"> 

Merci pour votre aide!

Répondre

4

Si vous passez dans un paramètre externe à un stylesheet, la déclaration xsl:param doit être un enfant de xsl:stylesheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:param name="projID" /> 

    <xsl:template match="/"> 
     <xsl:for-each select="list/project[@id=$projID]/observation"> 
      <tr> 
       <td> 
        <xsl:value-of select="@id" /> 
       </td> 
      </tr> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

Le paramètre est alors global, et peut être utilisé tout au long de la feuille de style.

+0

Wow, super! Merci pour cette réponse très rapide. Travaille pour moi! – Nico