For-each dans un autre For-Each et accéder à un attribut défini dans la première boucle à l'aide de la valeur de la seconde

StackOverflow https://stackoverflow.com/questions/1200936

  •  05-07-2019
  •  | 
  •  

Question

Pour chaque collection d'en-têtes et recherchez un attribut donné dans une autre collection d'éléments. Si j'ai quelque chose comme:

<ROOT>  
  <Listings> 
    <Listing>
      <Headers>
        <Header width="3cm" value="UserName" />
        <Header width="3cm" value="MobileAlias" />
        <Header width="3cm" value="Name" />
        <Header width="3cm" value="Email" />
        <Header width="1cm" value="Gender" />
        <Header width="2cm" value="LastLoginDate" />
        <Header width="2cm" value="LastActivityDate" />
        <Header width="1.5cm" value="IsApproved" />
        <Header width="1.5cm" value="IsLockedOut" />
      </Headers>
      <Footers></Footers>
      <Items>
        <Item UserName="Admin" MobileAlias="Admin" Name="Systems Administrator" Email="388354123@foo.com" Gender="Male" LastLoginDate="29-07-2009 12:54:59" LastActivityDate="29-07-2009 12:56:37" IsApproved="True" IsLockedOut="False" />
        <Item UserName="Guest" MobileAlias="Guest" Name="Anonymous User" Email="1516626590@foo.com" Gender="Male" LastLoginDate="" LastActivityDate="" IsApproved="True" IsLockedOut="False" />
      </Items>
    </Listing>
  </Listings>
</ROOT>

Comment puis-je obtenir quelque chose comme:

  <fo:table-row>
    <fo:table-cell><fo:block>Admin</fo:block></fo:table-cell>
    <fo:table-cell><fo:block>Admin</fo:block></fo:table-cell>
    <fo:table-cell><fo:block>System Administrator</fo:block></fo:table-cell>
    <fo:table-cell><fo:block>388354123@foo.com</fo:block></fo:table-cell>
    <fo:table-cell><fo:block>Male</fo:block></fo:table-cell>
    <fo:table-cell><fo:block>29-07-2009 12:54:59</fo:block></fo:table-cell>
    <fo:table-cell><fo:block>29-07-2009 12:56:37</fo:block></fo:table-cell>
    <fo:table-cell><fo:block>True</fo:block></fo:table-cell>
    <fo:table-cell><fo:block>True</fo:block></fo:table-cell>
  </fo:table-row>
  (...)

Je veux dire, pour chaque élément dans les éléments, pour chaque en-tête dans les en-têtes, renvoyer l'attribut En-tête @ valeur de l'élément

Très apprécié.

Était-ce utile?

La solution

Je recommande d'utiliser des modèles séparés pour une facilité de maintenance accrue. Ce qui suit est fondamentalement ce que Anthony WJones a fait, mais adapté à vos commentaires:

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

  <xsl:output method='xml' indent="yes" /> 

  <!-- <Listing> elements become tables -->
  <xsl:template match="Listing">
    <fo:table>
      <xsl:apply-templates />
    </fo:table>
  </xsl:template>

  <!-- <Items> elements become table rows -->
  <xsl:template match="Items">
    <fo:table-row>
      <xsl:apply-templates />
    </fo:table-row>
  </xsl:template>

  <!-- applies the correct order to the output -->
  <xsl:template match="Item">
    <fo:table-row>
      <xsl:variable name="this" select="." />
      <xsl:for-each select="../../Headers/Header">
        <xsl:apply-templates select="$this/@*[name() = current()/@value]" />
      </xsl:for-each>
    </fo:table-row>
  </xsl:template>

  <!-- <Item> attributes become table cells -->
  <xsl:template match="Item/@*">
    <fo:table-cell>
      <fo:block>
        <xsl:value-of select="." />
      </fo:block>
    </fo:table-cell>
  </xsl:template>

  <xsl:template match="text()" />

</xsl:stylesheet>

Autres conseils

Je ne connais pas xsl-fo mais ce n'est pas pertinent. Voici un exemple simple générant un tableau HTML: -

<xsl:template match="/ROOT">
  <table rules="all">
    <xsl:for-each select="Listings/Listing/Items/Item">
      <xsl:variable name="item" select="." /> 
      <tr>
        <xsl:for-each select="/ROOT/Listings/Listing/Headers/Header">
          <td><xsl:value-of select="$item/@*[local-name()=current()/@value]" /></td>
        </xsl:for-each>
      </tr>
    </xsl:for-each>
  </table>
</xsl:template>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top