在重复节点集中返回“最新”节点

时间:2012-09-11 17:38:41

标签: xml xpath repeat

我需要调整以下XPath表达式以从示例XML(来自MS InfoPath)返回“Latest”Amend_Start_Date

//my:Amend_Data[0=count(following-sibling::my:Amend_Data)]//my:Amend_Start_Date

和XML:

<?xml version="1.0" encoding="UTF-8"?><?mso-infoPathSolution solutionVersion="1.0.0.440" productVersion="14.0.0" PIVersion="1.0.0.0" href="file:///C:\Documents%20and%20Settings\Chris\Local%20Settings\Application%20Data\Microsoft\InfoPath\Designer3\1c02663d7ed84d09\manifest.xsf" ?><?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?><?mso-infoPath-file-attachment-present?><my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-05-05T19:56:08" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
<my:MasterSection>
    <my:Planning_Section>
        <my:Planned_Start_Date>2012-09-12</my:Planned_Start_Date>
        <my:Planned_End_Date>2012-09-14</my:Planned_End_Date>
    </my:Planning_Section>
    <my:Actual_Section>
        <my:Actual_Start_Date>2012-09-13</my:Actual_Start_Date>
        <my:Actual_End_Date>2012-09-15</my:Actual_End_Date>
    </my:Actual_Section>
    <my:Amend_Hider>
        <my:Amend_Info_Repeater_Group>
            <my:Amend_Info_Repeater>
                <my:Amend_Data>
                    <my:Amend_Start_Date>2012-09-16</my:Amend_Start_Date>
                    <my:Amend_End_Date>2012-09-21</my:Amend_End_Date>
                </my:Amend_Data>
            </my:Amend_Info_Repeater>
            <my:Amend_Info_Repeater>
                <my:Amend_Data>
                    <my:Amend_Start_Date>2012-09-23</my:Amend_Start_Date>
                    <my:Amend_End_Date>2012-09-27</my:Amend_End_Date>
                </my:Amend_Data>
            </my:Amend_Info_Repeater>
        </my:Amend_Info_Repeater_Group>
    </my:Amend_Hider>
</my:MasterSection>

我上周四开始使用XPath ...如果我需要勺子喂食,请原谅我。如上所示,列出的表达式从示例XML返回所有开始日期节点。它旨在返回“Latest”Amend_Start_Date,但不是因为XML的结构使得Amend_Start_Date节点没有组合在一起。在此示例中,表示正确的表达式将返回2012-09-23的开始日期和 2012-09-16

必须有办法实现这一目标!不知何故,必须修改Amend_Data节点的相对路径...感谢任何帮助!

PS:这与我周五发布的can be found here问题有关。您可能会注意到该问题中列出的示例XML中的结构差异,即此问题。

3 个答案:

答案 0 :(得分:3)

此:

(//my:Amend_Data)[last()]//my:Amend_Start_Date 

应该这样做。

不使用last(),您可以尝试:

//my:Amend_Info_Repeater[0=count(following-sibling::my:Amend_Info_Repeater)]/my:Amend_Data/my:Amend_Start_Date 

答案 1 :(得分:1)

使用

(//my:Amend_Start_Date)[last()]

这将选择XML文档中所有my:Amend_Start_Date元素的最后一个(按文档顺序)。

<强>更新

如果由于InfoPath的XPath实现中的某些错误/不合规,无法成功/正确评估上述表达式,请尝试:

//my:Amend_Start_Date
    [not(preceding::my:Amend_Start_Date or ancestor::my:Amend_Start_Date)]

答案 2 :(得分:0)

我没有在一个xpath中得到它,但也许xslt有帮助。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-05-05T19:56:08">
    <xsl:output method = "text" indent="yes"/>

    <xsl:variable name="y" select="//my:Amend_Data"/>
    <xsl:variable name="x" select="$y[count($y)]/my:Amend_Start_Date"/>

  <xsl:template match="/">
        <xsl:value-of select="$x"/>
  </xsl:template>
</xsl:stylesheet>