2014-12-04 184 views
1

如何在XSL中選擇具有特定屬性的節點的位置?

具有屬性的節點的位置

XML:

<document type="async" page="tabTabel"> 
<tabs> 
    <table> 
    <row type="header"> 
    <column type="text">href</column> 
    <column type="number">Mapnr.</column> 
    <column type="mapposition">Nr.</column> 
    <column type="text">Description</column> 
    <column type="text">Document</column> 
    <column type="date">Date</column> 
    </row> 

    <row type="data"> 
    <column><![CDATA[]]></column> 
    <column><![CDATA[10]]></column> 
    <column><![CDATA[17]]></column> 
    <column><![CDATA[Documentation may 2013 .pdf]]></column> 
    <column><![CDATA[Documentation may 2013 .pdf]]></column> 
    <column><![CDATA[03-04-2014]]></column> 
    </row> 

    </table> 
</tabs> 
</document> 

目前沒有工作的XSLT:

<xsl:template match="tabs//row[@type='data']> 
<xsl:variable name="mapnumber"> 
    <xsl:value-of select="../row[@type='header']/column[@type='mapposition'][position()]" /> 
</xsl:variable> 
</xsl:template> 

我想用型 'mapposition' 列的索引號/位置。我怎樣才能做到這一點?

回答

2

請嘗試:

<xsl:variable name="mapnumber" select="count(../row[@type='header']/column[@type='mapposition']/preceding-sibling::column) + 1" /> 

鑑於你的編輯,你可能想要做的事,如:

<xsl:template match="table"> 
    <xsl:variable name="mapnumber" select="count(row[@type='header']/column[@type='mapposition']/preceding-sibling::column) + 1" /> 
    <xsl:value-of select="row[@type='data']/column[$mapnumber]" /> 
</xsl:template> 
+0

對不起,這確實做了我需要的。非常感謝!它是如何工作的? – Grafit 2014-12-04 12:51:39

+0

它計算選定列之前的列(在同一行中)。你的例子中有兩個(文本和數字)。加+1,你得到索引位置= 3。 – 2014-12-04 13:00:21

0

這個例子可以幫助你:

如果您已經將下面的XML

<row type='header'> 
<column type='text'>a</column> 
<column type='mapposition'>b</column> 
<column type='number'>c</column> 
</row> 

要獲得column的位置@type = 'mapposition',你可以使用這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 
<xsl:template match="row[@type='header']"> 
    <xsl:apply-templates select="column"/> 
</xsl:template> 

<xsl:template match="column"> 
    <xsl:if test="@type='mapposition'"><xsl:value-of select="position()"/></xsl:if> 
</xsl:template> 
</xsl:stylesheet> 
+0

我真的不知道如何運用這一點,因爲我需要在我的 Grafit 2014-12-04 12:36:06

相關問題