5

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

目的是根据兄弟节点的值来选择一个节点——在本例中是基于 Pagetype 节点的值的 Pagetitle 节点。

路径:

/dsQueryResponse/Rows/Row/@Title
/dsQueryResponse/Rows/Row/@Pagetype
/dsQueryResponse/Rows/Row/@Pagetitle

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

<xsl:value-of select= "/dsQueryResponse/Rows/Row[Pagetype='Parent']/@Pagetitle" />  

示例 xml:

<dsQueryResponse>
       <Rows>
            <Row>
               <Title>1</Title>
               <Pagetype>Parent</Pagetype>
               <Pagetitle>title of page</Pagetitle>
            </Row>
        </Rows>
</dsQueryResponse>  

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

4

3 回答 3

3

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

<xsl:value-of select= "/dsQueryResponse/Rows/Row[@Pagetype='Parent']/@Pagetitle" />

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

于 2011-10-19T17:27:02.403 回答
0

以下是我对问题的理解:查找每个 Row 的子元素 Pagetype 为“Parent”,显示子元素 Pagetitle 的值。

我的解决方案:创建满足条件的所有行的节点集,然后选择子元素 Pagetitle 的值。

<xsl:for-each select="/dsQueryResponse/Rows/Row[Pagetype='Parent']">
    <xsl:value-of select="Pagetitle" />
</xsl:for-each>
于 2014-12-30T02:30:57.580 回答
0

使用提供的 XML 文档使用

/*/*/*[Pagetype = 'Parent']/Pagetitle

基于 XSLT 的验证

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select="/*/*/*[Pagetype = 'Parent']/Pagetitle"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时

<dsQueryResponse>
       <Rows>
            <Row>
               <Title>1</Title>
               <Pagetype>Parent</Pagetype>
               <Pagetitle>title of page</Pagetitle>
            </Row>
        </Rows>
</dsQueryResponse>

计算 XPath 表达式并输出所有(在本例中只有一个)选定节点

<Pagetitle>title of page</Pagetitle>
于 2011-10-20T03:10:27.460 回答