469,576 Members | 1,770 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,576 developers. It's quick & easy.

Xslt/Xpath help

I have an xml and i'm trying to loop each node...
When i do FOR EACH i seem not to get my desired result
I want to loop through and get only the values that matches the question i
specified with the corresponding answers? I don't want to retrieve all the
Answer and VoteAnswers nodes

<xsl:for-each select="NSurveyVoter/Voter/Question/Answer">

<xsl:value-of select="Answer[Answer='&lt;/b&gt;What dont you like
about working for us?&lt;/b&gt;']/Answer/text()"/>

<xsl:value-of
select="NSurveyVoter/Voter/Question/Answer[Answer='&lt;/b&gt;What dont you
like about working for us?&lt;/b&gt;']/VoterAnswer"/>

</xsl:for-each>

But when i do :-

<xsl:for-each select="NSurveyVoter/Voter/Question/Answer">

<DIV STYLE="margin-left:1em">

<tr>

<td> <xsl:value-of select="Answer"/>

</td>

</tr>

<tr>

<td>

<b>

<xsl:value-of select="VoterAnswer"/>

</b>

</td>

</tr>

</DIV>

</xsl:for-each>

I get the result i want.

What am i missing?

My Xml below:-

<?xml version="1.0" encoding="utf-8" ?>

<?xml-stylesheet type="text/xsl" href="short.xsl"?>

<NSurveyVoter>

<Voter>

<VoterID>649</VoterID>

<SurveyID>13</SurveyID>

<VoteDate>2005-09-29T12:38:12.9700000+10:00</VoteDate>

<StartDate>2005-09-29T12:33:55.5330000+10:00</StartDate>

<IPSource>192.168.100.77</IPSource>

<Question>

<QuestionText />

<VoterID>649</VoterID>

<QuestionId>574</QuestionId>

<Answer>

<AnswerID>2062</AnswerID>

<Answer><b>What improvements would you like to see?(e.g Study support
etc...)</b></Answer>

<VoterAnswer>higher sunday pay rates.</VoterAnswer>

<VoterId>649</VoterId>

<QuestionId>574</QuestionId>

<SectionNumber>0</SectionNumber>

</Answer>

</Question>

<Question>

<QuestionText></QuestionText>

<VoterID>649</VoterID>

<QuestionId>575</QuestionId>

<Answer>

<AnswerID>2063</AnswerID>

<Answer><b>What is the best thing about working for us?</b></Answer>

<VoterAnswer>the relaxed friendly atmosphere.</VoterAnswer>

<VoterId>649</VoterId>

<QuestionId>575</QuestionId>

<SectionNumber>0</SectionNumber>

</Answer>

</Question>

<Question>

<QuestionText></QuestionText>

<VoterID>649</VoterID>

<QuestionId>576</QuestionId>

<Answer>

<AnswerID>2064</AnswerID>

<Answer>&lt;/b&gt;What dont you like about working for
us?&lt;/b&gt;</Answer>

<VoterAnswer>the product! very sick of telstra.</VoterAnswer>

<VoterId>649</VoterId>

<QuestionId>576</QuestionId>

<SectionNumber>0</SectionNumber>

</Answer>

</Question>

</Voter>

</NSurveyVoter>


Nov 21 '05 #1
5 1397
Thanks for the reply Swapna..
And when i applied what you adviced i could loop through the Queations and
answer accordingly.
But what i want is match a particular question and answer e.g

<Answer><b>What improvements would you like to see?(e.g Study support
etc...)</b></Answer

I don't want to retrieve all the Questions and answer just the one above or
maybe any match i give to it.
Thanks

"swapna guddanti [MSFT]" wrote:

"Patrick.O.Ige" <pa********@optusnet.com.au> wrote in message
news:Ox**************@tk2msftngp13.phx.gbl...
I have an xml and i'm trying to loop each node...
When i do FOR EACH i seem not to get my desired result
I want to loop through and get only the values that matches the question i
specified with the corresponding answers? I don't want to retrieve all the
Answer and VoteAnswers nodes

<xsl:for-each select="NSurveyVoter/Voter/Question/Answer">

<xsl:value-of select="Answer[Answer='</b>What dont you like
about working for us?</b>']/Answer/text()"/>

<xsl:value-of
select="NSurveyVoter/Voter/Question/Answer[Answer='</b>What dont you
like about working for us?</b>']/VoterAnswer"/>

</xsl:for-each>

But when i do :-

<xsl:for-each select="NSurveyVoter/Voter/Question/Answer">

<DIV STYLE="margin-left:1em">

<tr>

<td> <xsl:value-of select="Answer"/>

</td>

</tr>

<tr>

<td>

<b>

<xsl:value-of select="VoterAnswer"/>

</b>

</td>

</tr>

</DIV>

</xsl:for-each>

I get the result i want.

What am i missing?

My Xml below:-

<?xml version="1.0" encoding="utf-8" ?>

<?xml-stylesheet type="text/xsl" href="short.xsl"?>

<NSurveyVoter>

<Voter>

<VoterID>649</VoterID>

<SurveyID>13</SurveyID>

<VoteDate>2005-09-29T12:38:12.9700000+10:00</VoteDate>

<StartDate>2005-09-29T12:33:55.5330000+10:00</StartDate>

<IPSource>192.168.100.77</IPSource>

<Question>

<QuestionText />

<VoterID>649</VoterID>

<QuestionId>574</QuestionId>

<Answer>

<AnswerID>2062</AnswerID>

<Answer><b>What improvements would you like to see?(e.g Study support
etc...)</b></Answer>

<VoterAnswer>higher sunday pay rates.</VoterAnswer>

<VoterId>649</VoterId>

<QuestionId>574</QuestionId>

<SectionNumber>0</SectionNumber>

</Answer>

</Question>

<Question>

<QuestionText></QuestionText>

<VoterID>649</VoterID>

<QuestionId>575</QuestionId>

<Answer>

<AnswerID>2063</AnswerID>

<Answer><b>What is the best thing about working for us?</b></Answer>

<VoterAnswer>the relaxed friendly atmosphere.</VoterAnswer>

<VoterId>649</VoterId>

<QuestionId>575</QuestionId>

<SectionNumber>0</SectionNumber>

</Answer>

</Question>

<Question>

<QuestionText></QuestionText>

<VoterID>649</VoterID>

<QuestionId>576</QuestionId>

<Answer>

<AnswerID>2064</AnswerID>

<Answer></b>What dont you like about working for
us?</b></Answer>

<VoterAnswer>the product! very sick of telstra.</VoterAnswer>

<VoterId>649</VoterId>

<QuestionId>576</QuestionId>

<SectionNumber>0</SectionNumber>

</Answer>

</Question>

</Voter>

</NSurveyVoter>


if you want to group the question and all the corresponding answers from
voters , then the following xslt might help.
you need to use a new template to loop over the answers for the particular
question since the docuemnt order is always top down inside a template.
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<xsl:for-each select="//NSurveyVoter/Voter/Question/Answer/Answer">
<question>
<xsl:value-of select="."/>
<answers>
<xsl:call-template name="newTemplate">
<xsl:with-param name="question" select="."/>
</xsl:call-template>
</answers>
</question>
</xsl:for-each>
</xsl:template>
<xsl:template name="newTemplate">
<xsl:param name="question"/>
<xsl:for-each select="//NSurveyVoter/Voter/Question/Answer">
<answer>
<xsl:value-of select="VoterAnswer[../Answer = $question]"/>
</answer>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Nov 23 '05 #2
Patrick.O.Ige wrote:
I have an xml and i'm trying to loop each node...
Don't. Loops are for iterative processing out of document sequence,
not for finding things (well, usually...:-)
When i do FOR EACH i seem not to get my desired result
I want to loop through and get only the values that matches the
question i specified with the corresponding answers? I don't want to
retrieve all the Answer and VoteAnswers nodes


You haven't said what you want to do, but if you just want to
retrieve the answer to one specific question:

<xsl:value-of select="NSurveyVoter/Voter/Question/Answer/Answer
[contains(normalize-space(.),
'What dont you like about working for us?')]
/following-sibling::VoterAnswer"/>

In your example, this will return the value "the product! very sick of
telstra."

///Peter
Nov 23 '05 #3

"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
Thanks for the reply Swapna..
And when i applied what you adviced i could loop through the Queations and
answer accordingly.
But what i want is match a particular question and answer e.g

<Answer><b>What improvements would you like to see?(e.g Study support
etc...)</b></Answer

I don't want to retrieve all the Questions and answer just the one above or maybe any match i give to it.
Thanks

"swapna guddanti [MSFT]" wrote:

"Patrick.O.Ige" <pa********@optusnet.com.au> wrote in message
news:Ox**************@tk2msftngp13.phx.gbl...
I have an xml and i'm trying to loop each node...
When i do FOR EACH i seem not to get my desired result
I want to loop through and get only the values that matches the question i specified with the corresponding answers? I don't want to retrieve all the Answer and VoteAnswers nodes

<xsl:for-each select="NSurveyVoter/Voter/Question/Answer">

<xsl:value-of select="Answer[Answer='</b>What dont you like
about working for us?</b>']/Answer/text()"/>

<xsl:value-of
select="NSurveyVoter/Voter/Question/Answer[Answer='</b>What dont you
like about working for us?</b>']/VoterAnswer"/>

</xsl:for-each>

But when i do :-

<xsl:for-each select="NSurveyVoter/Voter/Question/Answer">

<DIV STYLE="margin-left:1em">

<tr>

<td> <xsl:value-of select="Answer"/>

</td>

</tr>

<tr>

<td>

<b>

<xsl:value-of select="VoterAnswer"/>

</b>

</td>

</tr>

</DIV>

</xsl:for-each>

I get the result i want.

What am i missing?

My Xml below:-

<?xml version="1.0" encoding="utf-8" ?>

<?xml-stylesheet type="text/xsl" href="short.xsl"?>

<NSurveyVoter>

<Voter>

<VoterID>649</VoterID>

<SurveyID>13</SurveyID>

<VoteDate>2005-09-29T12:38:12.9700000+10:00</VoteDate>

<StartDate>2005-09-29T12:33:55.5330000+10:00</StartDate>

<IPSource>192.168.100.77</IPSource>

<Question>

<QuestionText />

<VoterID>649</VoterID>

<QuestionId>574</QuestionId>

<Answer>

<AnswerID>2062</AnswerID>

<Answer><b>What improvements would you like to see?(e.g Study support etc...)</b></Answer>

<VoterAnswer>higher sunday pay rates.</VoterAnswer>

<VoterId>649</VoterId>

<QuestionId>574</QuestionId>

<SectionNumber>0</SectionNumber>

</Answer>

</Question>

<Question>

<QuestionText></QuestionText>

<VoterID>649</VoterID>

<QuestionId>575</QuestionId>

<Answer>

<AnswerID>2063</AnswerID>

<Answer><b>What is the best thing about working for us?</b></Answer>

<VoterAnswer>the relaxed friendly atmosphere.</VoterAnswer>

<VoterId>649</VoterId>

<QuestionId>575</QuestionId>

<SectionNumber>0</SectionNumber>

</Answer>

</Question>

<Question>

<QuestionText></QuestionText>

<VoterID>649</VoterID>

<QuestionId>576</QuestionId>

<Answer>

<AnswerID>2064</AnswerID>

<Answer></b>What dont you like about working for
us?</b></Answer>

<VoterAnswer>the product! very sick of telstra.</VoterAnswer>

<VoterId>649</VoterId>

<QuestionId>576</QuestionId>

<SectionNumber>0</SectionNumber>

</Answer>

</Question>

</Voter>

</NSurveyVoter>


if you want to group the question and all the corresponding answers from
voters , then the following xslt might help.
you need to use a new template to loop over the answers for the particular question since the docuemnt order is always top down inside a template.
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<xsl:for-each select="//NSurveyVoter/Voter/Question/Answer/Answer">
<question>
<xsl:value-of select="."/>
<answers>
<xsl:call-template name="newTemplate">
<xsl:with-param name="question" select="."/>
</xsl:call-template>
</answers>
</question>
</xsl:for-each>
</xsl:template>
<xsl:template name="newTemplate">
<xsl:param name="question"/>
<xsl:for-each select="//NSurveyVoter/Voter/Question/Answer">
<answer>
<xsl:value-of select="VoterAnswer[../Answer = $question]"/>
</answer>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


hi,
If you want to group "a particular question" and the corresponding answers
then the following will do that.
Notice that I have used "normalize-space" function to take care of spaces in
hard coded strings.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="newTemplate">
<xsl:variable name="question" select="'What improvements would you like
to see?(e.g Study support
etc...)'"/>
<xsl:for-each select="//NSurveyVoter/Voter/Question/Answer">
<xsl:if test="normalize-space(VoterAnswer/../Answer) =
normalize-space($question)">
<answer>
<xsl:value-of select="VoterAnswer"/>
</answer>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

thanks,
swapna
Nov 23 '05 #4
Thx for the reply Swapna and Peter but with what you sent me i still
retrieve all the Questions with the coresponding answers..
I came up with an xslt that does what i want below:-
But i have a problem when i display it on the browser i get a BIG GAP between
The "HELLO" in the first row and the second ROW
Any advice on this..
Thanks alot

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<BODY>
<TABLE BORDER="1" width="800px" cellspacing="1" cellpadding="1">
<tr><td>hello</td></tr>
<xsl:for-each select="NSurveyVoter/Voter/Question/Answer">
<tr>
<td>
<xsl:value-of select="../Answer[Answer='<b>What dont you like about
working for CrazyJohns?</b>']/Answer/text()"/>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="../Answer[Answer='<b>What dont you like about
working for CrazyJohns?</b>']/VoterAnswer"/>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="../Answer[Answer='<b>What is the best thing
about working for Crazy Johns?</b>']/Answer/text()"/>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="../Answer[Answer='<b>What is the best thing
about working for Crazy Johns?</b>']/VoterAnswer"/>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="../Answer[Answer='<b>What improvements would
you like to see?(e.g Study support etc...)</b>']/Answer/text()"/>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="../Answer[Answer='<b>What improvements would
you like to see?(e.g Study support etc...)</b>']/VoterAnswer"/>
</td>
</tr>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>

</xsl:stylesheet>




"swapna guddanti [MSFT]" wrote:

"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
Thanks for the reply Swapna..
And when i applied what you adviced i could loop through the Queations and
answer accordingly.
But what i want is match a particular question and answer e.g

<Answer><b>What improvements would you like to see?(e.g Study support
etc...)</b></Answer

I don't want to retrieve all the Questions and answer just the one above

or
maybe any match i give to it.
Thanks

"swapna guddanti [MSFT]" wrote:

"Patrick.O.Ige" <pa********@optusnet.com.au> wrote in message
news:Ox**************@tk2msftngp13.phx.gbl...
> I have an xml and i'm trying to loop each node...
> When i do FOR EACH i seem not to get my desired result
> I want to loop through and get only the values that matches the question i > specified with the corresponding answers? I don't want to retrieve all the > Answer and VoteAnswers nodes
>
> <xsl:for-each select="NSurveyVoter/Voter/Question/Answer">
>
> <xsl:value-of select="Answer[Answer='</b>What dont you like
> about working for us?</b>']/Answer/text()"/>
>
> <xsl:value-of
> select="NSurveyVoter/Voter/Question/Answer[Answer='</b>What dont you
> like about working for us?</b>']/VoterAnswer"/>
>
> </xsl:for-each>
>
>
>
> But when i do :-
>
> <xsl:for-each select="NSurveyVoter/Voter/Question/Answer">
>
>
>
>
>
> <DIV STYLE="margin-left:1em">
>
> <tr>
>
> <td> <xsl:value-of select="Answer"/>
>
>
>
>
>
> </td>
>
> </tr>
>
>
>
> <tr>
>
> <td>
>
> <b>
>
> <xsl:value-of select="VoterAnswer"/>
>
>
>
> </b>
>
> </td>
>
> </tr>
>
>
>
>
>
> </DIV>
>
>
>
> </xsl:for-each>
>
> I get the result i want.
>
> What am i missing?
>
>
>
> My Xml below:-
>
> <?xml version="1.0" encoding="utf-8" ?>
>
> <?xml-stylesheet type="text/xsl" href="short.xsl"?>
>
> <NSurveyVoter>
>
>
>
> <Voter>
>
>
>
> <VoterID>649</VoterID>
>
> <SurveyID>13</SurveyID>
>
> <VoteDate>2005-09-29T12:38:12.9700000+10:00</VoteDate>
>
> <StartDate>2005-09-29T12:33:55.5330000+10:00</StartDate>
>
> <IPSource>192.168.100.77</IPSource>
>
> <Question>
>
> <QuestionText />
>
> <VoterID>649</VoterID>
>
> <QuestionId>574</QuestionId>
>
> <Answer>
>
> <AnswerID>2062</AnswerID>
>
> <Answer><b>What improvements would you like to see?(e.g Study support > etc...)</b></Answer>
>
> <VoterAnswer>higher sunday pay rates.</VoterAnswer>
>
> <VoterId>649</VoterId>
>
> <QuestionId>574</QuestionId>
>
> <SectionNumber>0</SectionNumber>
>
> </Answer>
>
> </Question>
>
>
>
> <Question>
>
> <QuestionText></QuestionText>
>
> <VoterID>649</VoterID>
>
> <QuestionId>575</QuestionId>
>
> <Answer>
>
> <AnswerID>2063</AnswerID>
>
> <Answer><b>What is the best thing about working for us?</b></Answer>
>
> <VoterAnswer>the relaxed friendly atmosphere.</VoterAnswer>
>
> <VoterId>649</VoterId>
>
> <QuestionId>575</QuestionId>
>
> <SectionNumber>0</SectionNumber>
>
> </Answer>
>
> </Question>
>
>
>
> <Question>
>
> <QuestionText></QuestionText>
>
> <VoterID>649</VoterID>
>
> <QuestionId>576</QuestionId>
>
> <Answer>
>
> <AnswerID>2064</AnswerID>
>
> <Answer></b>What dont you like about working for
> us?</b></Answer>
>
> <VoterAnswer>the product! very sick of telstra.</VoterAnswer>
>
> <VoterId>649</VoterId>
>
> <QuestionId>576</QuestionId>
>
> <SectionNumber>0</SectionNumber>
>
> </Answer>
>
> </Question>
>
> </Voter>
>
>
>
>
>
> </NSurveyVoter>
>
>
>
>
>
>
if you want to group the question and all the corresponding answers from
voters , then the following xslt might help.
you need to use a new template to loop over the answers for the particular question since the docuemnt order is always top down inside a template.
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<xsl:for-each select="//NSurveyVoter/Voter/Question/Answer/Answer">
<question>
<xsl:value-of select="."/>
<answers>
<xsl:call-template name="newTemplate">
<xsl:with-param name="question" select="."/>
</xsl:call-template>
</answers>
</question>
</xsl:for-each>
</xsl:template>
<xsl:template name="newTemplate">
<xsl:param name="question"/>
<xsl:for-each select="//NSurveyVoter/Voter/Question/Answer">
<answer>
<xsl:value-of select="VoterAnswer[../Answer = $question]"/>
</answer>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


hi,
If you want to group "a particular question" and the corresponding answers
then the following will do that.
Notice that I have used "normalize-space" function to take care of spaces in
hard coded strings.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="newTemplate">
<xsl:variable name="question" select="'What improvements would you like
to see?(e.g Study support
etc...)'"/>
<xsl:for-each select="//NSurveyVoter/Voter/Question/Answer">
<xsl:if test="normalize-space(VoterAnswer/../Answer) =
normalize-space($question)">
<answer>
<xsl:value-of select="VoterAnswer"/>
</answer>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

thanks,
swapna

Nov 23 '05 #5
Patrick.O.Ige wrote:
Thx for the reply Swapna and Peter but with what you sent me i still
retrieve all the Questions with the coresponding answers.
Not with what I posted, because I tested it on the XML file you
supplied, and it returned exactly the string value of VoterAnswer.

Or is there something else about your document you didn't show?
Like multiple questions with the same Answer/Answer value?
I came up with an xslt that does what i want below:-
But i have a problem when i display it on the browser i get a BIG GAP
between The "HELLO" in the first row and the second ROW
Any advice on this..


I have no idea what you are trying to do here, sorry.

///Peter
Nov 24 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Justine Hlista | last post: by
4 posts views Thread by Son KwonNam | last post: by
4 posts views Thread by Chris Kettenbach | last post: by
3 posts views Thread by Ian Roddis | last post: by
1 post views Thread by Sergey Dubinets | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.