开发者

Preceding sibling in XML

I have a XML data like that:

<items>
<data>2</data>  
<listElement>       
    <amounts>           
        <period_id>1</period_id>            
        <amount>5</amount>
    </amounts>
    <amounts>
        <period_id>2</period_id>            
        <amount>6</amount>
    </amounts>
    <amounts>           
        <period_id>3</period_id>            
        <amount>7</amount>
    </amounts>
    <amounts>           
        <period_id>8</period_id>            
        <amount>89</amount>
    </amounts>
</listElement>
</items>
<items>
<data></data>   
<listElement>       
    <amounts>           
        <period_id>4</period_id>            
        <amount>55</amount>
    </amounts>
    <amounts>
        <period_id>5</period_id>            
        <amount>9</amount>
    </amounts>
    <amounts>           
        <period_id>6</period_id>            
        <amount>20</amount>
    </amounts>
    <amounts>           
        <period_id>7</period_id>            
        <amount>80</amount>
    </amounts>
</listElement>
</items>

In my xsl code I'm inside a开发者_运维技巧 node amounts and I want to get the value of the tag "data" who is parent of this tag amounts?

I'm using xalan with xslt1.0 and apache fop

Note: I tried with:

<xsl:value-of select="preceding-sibling::*data[normalize-space(.)]">
</xsl:value-of> 

But still wrong.


in my xsl code i'm inside a node amounts and i want to get the value of the tag "data" who is parent of this tag amounts

Do note that data element is a child of items (amount grandparent) in your sample.

Use this:

../../data

Also this:

preceding::data[1]

But it must be guaranteed that there is going to be one data in every items.

If you really want to use preceding-sibling axis then:

../preceding-sibling::data


From the context of an amounts element:

../preceding-sibling::data[1]

The following stylesheet copies the preceding data element into each amounts element, leaving the rest of the document unchanged:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="amounts">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
            <xsl:copy-of select="../preceding-sibling::data[1]" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

It produces the following output when applied to your source document:

<root>
    <items>
        <data>2</data>
        <listElement>
            <amounts>
                <period_id>1</period_id>
                <amount>5</amount>
                <data>2</data>
            </amounts>
            <amounts>
                <period_id>2</period_id>
                <amount>6</amount>
                <data>2</data>
            </amounts>
            <amounts>
                <period_id>3</period_id>
                <amount>7</amount>
                <data>2</data>
            </amounts>
            <amounts>
                <period_id>8</period_id>
                <amount>89</amount>
                <data>2</data>
            </amounts>
        </listElement>
    </items>
    <items>
        <data />
        <listElement>
            <amounts>
                <period_id>4</period_id>
                <amount>55</amount>
                <data />
            </amounts>
            <amounts>
                <period_id>5</period_id>
                <amount>9</amount>
                <data />
            </amounts>
            <amounts>
                <period_id>6</period_id>
                <amount>20</amount>
                <data />
            </amounts>
            <amounts>
                <period_id>7</period_id>
                <amount>80</amount>
                <data />
            </amounts>
        </listElement>
    </items>
</root>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜