XSLT如果条件满足,如何获取先前的属性值?

时间:2011-08-08 23:20:41

标签: xml xslt

XSLT如果满足条件,如何获取先前的属性值?

我试图获得所需的但没有成功,有人可以帮助我如何实现这个目标吗?

这是我的Zip.xml

<Zip>
        <ZipNotify Zip="1144" ZipNo="1" ZipTime="2010-09-02T11:15:30+10:00"/>
        <ZipDetail Zone="U" DepartZip="West" ArriveZip="West"/>
        <ZipCategoryDetail ZORE="false" />
        <ZipOrigin ZipOrigin="ABC002" Die="20:59:00"/>
        <ZipDestination ZipDestination="UVW001" Live="21:38:00"/>
        <ZipPath>
            <ZipSubject ZipSubjectType="Payed" ZipNum="1">
                <ZipRoute ZipLoc="ABC002" ZipStop="true" ZipDieTime="20:59:00" ZipDieTime1="20:59:00"/>
            </ZipSubject>
            <ZipSubject ZipSubjectType="Payed" ZipNum="2">
                <ZipRoute ZipLoc="BCD002" ZipStop="true" ZipLiveTime="21:00:40" ZipDieTime1="21:01:00"/>
                <ZipSpec Charge="false"/>
            </ZipSubject>
            <ZipSubject ZipSubjectType="Payed" ZipNum="3">
                <ZipRoute ZipLoc="CDE001" ZipStop="true" ZipLiveTime="21:03:40" ZipDieTime1="21:04:00"/>
                <ZipSpec Charge="true"/>
            </ZipSubject>
            <ZipSubject ZipSubjectType="Payed" ZipNum="4">
                <ZipRoute ZipLoc="DEF001" ZipStop="true" ZipLiveTime="21:05:40" ZipDieTime1="21:06:00"/>
                <ZipSpec Charge="true"/>
            </ZipSubject>
            <ZipSubject ZipSubjectType="Payed" ZipNum="5">
                <ZipRoute ZipLoc="EFG001" ZipStop="true" ZipLiveTime="21:07:40" ZipDieTime1="21:08:00"/>
                <ZipSpec Charge="true"/>
            </ZipSubject>
            <ZipSubject ZipSubjectType="Payed" ZipNum="5">
                <ZipRoute ZipLoc="UVW001" ZipStop="true" ZipLiveTime="21:38:00" ZipLiveTime1="21:38:00"/>
                <ZipSpec Charge="true"/>
            </ZipSubject>
        </ZipPath>
</Zip>

这是我的XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html>
    <head/>
        <body>
            <xsl:for-each select="/Zip/ZipPath/ZipSubject">
                <xsl:if test="/Zip/ZipPath/ZipSubject/ZipSpec/@Charge = 'true'">
                    <div>ZipLoc: <xsl:value-of select="/Zip/ZipPath/ZipSubject/ZipRoute/@ZipLoc"/> </div>
                    <div>Charge: <xsl:value-of select="/Zip/ZipPath/ZipSubject/ZipSpec/@Charge"/></div>
                </xsl:if>

            </xsl:for-each>
        </body>
    </html>
    </xsl:template>
</xsl:stylesheet>

Out Put I want is 我想获得之前ZipSubject的ZipLoc,其中Charge =“true” 例如。对于ZipNum =“3”值Charge =“true”所以我的输出必须显示前一个ZipLoc =“BCD002”(即使Charge =“false”ZipNum =“2”)就是这样,它不应该对ZipOrigin进行进一步的验证对于ZipDestination,它应该从最后一个ZipNum =“5”检查如果Charge =“true”那么它应该使用相同的ZipLoc =“UVW001”

所需的输出应

BCD002UVW001

但我的输出如下。我尝试了可能的方法(我知道它缺乏XSLT的经验,这不是一个借口)请帮助我获得所需的输出

ZipLoc: ABC002
Charge: false
ZipLoc: ABC002
Charge: false
ZipLoc: ABC002
Charge: false
ZipLoc: ABC002
Charge: false
ZipLoc: ABC002
Charge: false
ZipLoc: ABC002
Charge: false

2 个答案:

答案 0 :(得分:1)

  

我想获得之前ZipSubject的ZipLoc,其中Charge =“true”

xsl:for-each的上下文中,您可以使用以下XPath:

 "preceding-sibling::ZipSubject[1]/ZipRoute/@ZipLoc"

例如,以下XSLT片段显示了如何仅为Charge ='true'的那些打印前一节点的ZipLog和Charge:

<xsl:for-each select="/Zip/ZipPath/ZipSubject[ZipSpec/@Charge = 'true']">
  <div>ZipLoc: <xsl:value-of 
       select="preceding-sibling::ZipSubject[1]/ZipRoute/@ZipLoc"/> 
  </div>
  <div>Charge: <xsl:value-of 
       select="preceding-sibling::ZipSubject[1]/ZipSpec/@Charge"/></div>
</xsl:for-each>

答案 1 :(得分:0)

好像你可能做的工作超出了你的需要。下面的xPath应该为你提到的第一个案例提供ZipLoc。

/Zip/ZipPath/ZipSubject[@ZipNum = '3' AND ZipSpec/@Charge = 'true']/ZipRoute/@ZipLoc