5

Similar problem to this question: XPath: select a node based on another node?

The object is to select a node based on the value of a sibling node-in this case the Pagetitle node based on the value of the Pagetype node.

xpaths:

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

This xsl isn't returning anything:

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

Sample xml:

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

The goal is to have the values of Pagetitle returned if they have a Pagetype value of "Parent".

Community
  • 1
  • 1
matt
  • 2,690
  • 2
  • 16
  • 17
  • Please show us a sample of the input XML and explain which nodes you want to select. Your current predicate `[Pagetype='Parent']` checks for child element(s) of the name `Pagetype` having the value `Parent`. – Martin Honnen Oct 19 '11 at 16:57
  • @Martin Added sample of xml and tried to clarify the nodes I want to select. – matt Oct 19 '11 at 19:09

3 Answers3

3

The @ sign denotes attributes of a node. So if you want to return the value of the attribute Pagetitle where the Pagetype attribute is equal to Parent, it should read:

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

A helpful resource I use to test my XPATH is http://www.xmlme.com/XpathTool.aspx

Aaron Prince
  • 170
  • 7
  • Thanks for the link. That will come in handy. I added a snippet of the xml to clarify. Pagetype and Pagetitle are both elements/nodes and have no attributes. I guess I get confused at to when the @ should be used. The xpath Sharepoint Designer spits out includes it for the Pagetile element. – matt Oct 19 '11 at 19:22
  • @_Aaron: Seems the OP added the source XML after your answer and there is no `Pagetype` attribute in it. – Dimitre Novatchev Oct 20 '11 at 12:33
0

Here's how I understand the problem: Find each Row with a child element Pagetype of 'Parent', display value of child element Pagetitle.

My solution: Create a node set of all Rows that fulfill the criteria then select value of child element Pagetitle.

<xsl:for-each select="/dsQueryResponse/Rows/Row[Pagetype='Parent']">
    <xsl:value-of select="Pagetitle" />
</xsl:for-each>
cathy
  • 1
0

With the provided XML document use:

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

XSLT - based verification:

<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>

when this transformation is applied on the provided XML document:

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

the XPath expression is evaluated and all (in this case just one) selected nodes are output:

<Pagetitle>title of page</Pagetitle>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • I tried that and the output is still nothing. Even a simple value-of returns nothing. It does all work if I filter with xsl:ifs and don't use predicates, so I'm pretty sure the xpaths are correct. Maybe it's something to do with the way Sharepoint reads the xsl. – matt Oct 20 '11 at 19:14