I would like to write out 4 questions whether they exist or not.So far my approach has been to select=”//NTE_NotesAndCommentsSegment_2/NTE_3_Comment” which retrieves all 3 comments. But I am having trouble with
-
Selecting
NTE_3_Commentwhere it contains “Question 1”(String Value) -
Writing out Question 4 when the question does not exist.
-
I also need to output the correct number for the
SETID.
Note: The question does not actually have the number in it. I am using the ID to sort the output.
Input XML:
<NTE_NotesAndCommentsSegment_2>
<NTE_1_SetIdNotesAndComments>1</NTE_1_SetIdNotesAndComments>
<NTE_2_SourceOfComment></NTE_2_SourceOfComment>
<NTE_3_Comment>Question 1 ? Answer 1</NTE_3_Comment>
</NTE_NotesAndCommentsSegment_2>
<NTE_NotesAndCommentsSegment_2>
<NTE_1_SetIdNotesAndComments>1</NTE_1_SetIdNotesAndComments>
<NTE_2_SourceOfComment></NTE_2_SourceOfComment>
<NTE_3_Comment>Question 2 ? Answer 2</NTE_3_Comment>
</NTE_NotesAndCommentsSegment_2>
<NTE_NotesAndCommentsSegment_2>
<NTE_1_SetIdNotesAndComments>1</NTE_1_SetIdNotesAndComments>
<NTE_2_SourceOfComment></NTE_2_SourceOfComment>
<NTE_3_Comment>Question 3? Answer 3</NTE_3_Comment>
</NTE_NotesAndCommentsSegment_2>
Expected Output XML:
<NTE_NotesAndCommentsSegment_2>
<NTE_1_SetIdNotesAndComments>1</NTE_1_SetIdNotesAndComments>
<NTE_2_SourceOfComment></NTE_2_SourceOfComment>
<NTE_3_Comment>Question 1 ? Answer 1</NTE_3_Comment>
</NTE_NotesAndCommentsSegment_2>
<NTE_NotesAndCommentsSegment_2>
<NTE_1_SetIdNotesAndComments>2</NTE_1_SetIdNotesAndComments>
<NTE_2_SourceOfComment></NTE_2_SourceOfComment>
<NTE_3_Comment>Question 2 ? Answer 2</NTE_3_Comment>
</NTE_NotesAndCommentsSegment_2>
<NTE_NotesAndCommentsSegment_2>
<NTE_1_SetIdNotesAndComments>3</NTE_1_SetIdNotesAndComments>
<NTE_2_SourceOfComment></NTE_2_SourceOfComment>
<NTE_3_Comment>Question 3 ? Answer 3</NTE_3_Comment>
</NTE_NotesAndCommentsSegment_2>
<NTE_NotesAndCommentsSegment_2>
<NTE_1_SetIdNotesAndComments>4</NTE_1_SetIdNotesAndComments>
<NTE_2_SourceOfComment></NTE_2_SourceOfComment>
<NTE_3_Comment>Question 4 ? *Blank* </NTE_3_Comment>
</NTE_NotesAndCommentsSegment_2>
I am looking for suggestions that can help me change my approach to this solution. Thanks in Advance.
Solution: Thanks to @O.R.Mapper suggestion. All four questions will be written out every time whether the question exist or not. The answers will display blank if the question does not exist in the source.
<NTE_NotesAndCommentsSegment_2>
<NTE_1_SetIdNotesAndComments>1</NTE_1_SetIdNotesAndComments>
<NTE_3_Comment>Question 1 ?<xsl:value-of select="substring-after(//NTE_NotesAndCommentsSegment_2/NTE_3_Comment[starts-with(text(),'Question 1')],'?')"/>
</NTE_3_Comment>
</NTE_NotesAndCommentsSegment_2>
<NTE_NotesAndCommentsSegment_2>
<NTE_1_SetIdNotesAndComments>2</NTE_1_SetIdNotesAndComments>
<NTE_3_Comment>Question 2 ?<xsl:value-of select="substring-after(//NTE_NotesAndCommentsSegment_2/NTE_3_Comment[starts-with(text(),'Question 2')],'?')"/>
</NTE_3_Comment>
</NTE_NotesAndCommentsSegment_2>
<NTE_NotesAndCommentsSegment_2>
<NTE_1_SetIdNotesAndComments>3</NTE_1_SetIdNotesAndComments>
<NTE_3_Comment>Question 3 ?<xsl:value-of select="substring-after(//NTE_NotesAndCommentsSegment_2/NTE_3_Comment[starts-with(text(),'Question 3')],'?')"/>
</NTE_3_Comment>
</NTE_NotesAndCommentsSegment_2>
<NTE_NotesAndCommentsSegment_2>
<NTE_1_SetIdNotesAndComments>4</NTE_1_SetIdNotesAndComments>
<NTE_3_Comment>Question 4 ? <xsl:value-of select="substring-after(//NTE_NotesAndCommentsSegment_2/NTE_3_Comment[starts-with(text(),'Question 4')],'?')"/>
</NTE_3_Comment>
</NTE_NotesAndCommentsSegment_2>
The solution for (1) would be the following XPath expression: