我有一些XML标记,如下所示:
<Conf_Standings League="Atlantic Coast">
<Listing>
<Team>Syracuse</Team>
<TeamID>C76</TeamID>
<Record Type="Conference">
<Wins>10</Wins>
<Loss>0</Loss>
<Win_Percentage>1.000</Win_Percentage>
</Record>
<Record Type="Overall">
<Wins>23</Wins>
<Loss>0</Loss>
<Win_Percentage>1.000</Win_Percentage>
</Record>
</Listing>
<Listing>
<Team>Virginia</Team>
<TeamID>D05</TeamID>
<Record Type="Conference">
<Wins>10</Wins>
<Loss>1</Loss>
<Win_Percentage>.909</Win_Percentage>
</Record>
<Record Type="Overall">
<Wins>19</Wins>
<Loss>5</Loss>
<Win_Percentage>.792</Win_Percentage>
</Record>
</Listing>
</Conf_Standings>
当我使用XSLT 1.0遍历列表时,我使用如下内容:
<xsl:for-each select="Conf_Standings/Listing">
我遇到的问题是,此时我目前处于每个人Listing
的上下文中,并且无法再引用League
中的Conf_Standings
属性。如果我尝试做以下事情:
"standings": [<xsl:for-each select="Conf_Standings/Listing">
{
"name": "<xsl:value-of select="normalize-space(@League)"/>"
}
...
联盟名称空洞。在上面的示例中抓住@League
的正确方法是什么,以便在Conf_Standings/Listing
答案 0 :(得分:1)
您可以使用父轴并获取@Leage
<xsl:value-of select="normalize-space(../@League)"/>
答案 1 :(得分:0)
此外,您可以在for-each之前使用变量来存储值并使用它:
<xsl:template match="/">
<xsl:variable name="attribute-leage" select="Conf_Standings/@League"/>
<xsl:for-each select="Conf_Standings/Listing">
... <xsl:value-of select="normalize-space($attribute-leage)"/> ...
</xsl:for-each>
</xsl:template>