使用 xsl 谓词根据另一个节点的值选择一个节点

mat*_*att 5 xml xslt sharepoint xpath predicates

与此问题类似的问题: XPath: select a node based on another node?

目标是根据同级节点的值选择一个节点——在本例中是基于 Pagetype 节点的值的 Pagetitle 节点。

路径:

/dsQueryResponse/Rows/Row/@Title
/dsQueryResponse/Rows/Row/@Pagetype
/dsQueryResponse/Rows/Row/@Pagetitle
Run Code Online (Sandbox Code Playgroud)

这个 xsl 没有返回任何东西:

<xsl:value-of select= "/dsQueryResponse/Rows/Row[Pagetype='Parent']/@Pagetitle" />  
Run Code Online (Sandbox Code Playgroud)

示例 xml:

<dsQueryResponse>
       <Rows>
            <Row>
               <Title>1</Title>
               <Pagetype>Parent</Pagetype>
               <Pagetitle>title of page</Pagetitle>
            </Row>
        </Rows>
</dsQueryResponse>  
Run Code Online (Sandbox Code Playgroud)

目标是返回 Pagetitle 的值,如果它们的 Pagetype 值为“Parent”。

小智 3

@符号表示节点的属性。因此,如果要返回 Pagetitle 属性的值(其中 Pagetype 属性等于 Parent),则应为:

<xsl:value-of select= "/dsQueryResponse/Rows/Row[@Pagetype='Parent']/@Pagetitle" />
Run Code Online (Sandbox Code Playgroud)

我用来测试 XPATH 的有用资源是http://www.xmlme.com/XpathTool.aspx