2009-07-29 63 views
2

對於每個標題集合並在另一個Items集合中找到某個屬性。如果我有這樣的:For-each在另一個For-Each中,並使用第二個循環的值訪問第一個循環中定義的屬性

<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="[email protected]" 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="[email protected]" Gender="Male" LastLoginDate="" LastActivityDate="" IsApproved="True" IsLockedOut="False" /> 
     </Items> 
    </Listing> 
    </Listings> 
</ROOT> 

我怎樣才能得到的東西,如:

<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>[email protected]</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> 
    (...) 

我的意思是,每個項目項目,換每個頁眉標頭,返回屬性項目的標頭@值

非常感謝。

+0

我會去,因爲清潔/模板解決方案的「託默勒格」解決方案,但你總是第一:d 真正因爲生命的救星整個下午我都把頭撞到了牆上。非常感激。 – 2009-07-30 08:51:23

回答

3

我建議使用單獨的模板來提高可維護性。下面基本上是什麼AnthonyWJones沒有,但適應您的輸入:

<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> 
+0

+1。 ...但你的實際工作完成是一個更好的解決方案。 ;) – AnthonyWJones 2009-07-29 20:05:22

2

我不知道xsl-fo,但那不相關。下面是一個生成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> 
+0

+1清潔和簡單。 – Tomalak 2009-07-29 16:14:54

相關問題