0

存在するかどうかにかかわらず、4 つの質問を書き出したいと思います。しかし、私は問題を抱えています

  1. 「質問 1」 (文字列値)NTE_3_Commentを含む場所を選択する

  2. 質問が存在しない場合、質問 4を書き出します。

  3. の正しい番号も出力する必要がありますSETID

注: 質問には実際には番号が含まれていません。ID を使用して出力を並べ替えています。

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

予期される出力 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>

このソリューションへのアプローチを変更するのに役立つ提案を探しています。前もって感謝します。

解決策: @ORMapper の提案に感謝します。質問が存在するかどうかにかかわらず、4 つの質問すべてが毎回書き出されます。質問がソースに存在しない場合、回答は空白で表示されます。

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

2 に答える 2

0

完全な xslt ソリューションは次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="root">
        <root>
            <xsl:call-template name="for.loop">
                <xsl:with-param name="i" select="1" />
                <xsl:with-param name="count" select="4" />
                <xsl:with-param name="answer" select="substring-after(//NTE_NotesAndCommentsSegment_2[position() = 1]/NTE_3_Comment, '?')" />
            </xsl:call-template>
        </root>
    </xsl:template>

    <xsl:template name="for.loop">

        <xsl:param name="i" />
        <xsl:param name="count" />
        <xsl:param name="answer" />

        <xsl:if test="$i &lt;= $count">
            <NTE_NotesAndCommentsSegment_2>
                <NTE_1_SetIdNotesAndComments>
                    <xsl:value-of select="$i" />
                </NTE_1_SetIdNotesAndComments>
                <NTE_2_SourceOfComment></NTE_2_SourceOfComment>
                <NTE_3_Comment>
                <xsl:choose>
                    <xsl:when test="$answer = ''">
                        <xsl:value-of select="concat('Question ', $i, ' ? *Blank*')" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat('Question ', $i, ' ? ', $comment)"/>
                    </xsl:otherwise>
               </xsl:choose>
               </NTE_3_Comment>
            </NTE_NotesAndCommentsSegment_2>
        </xsl:if>

        <!-- Repeat the loop until finished -->
        <xsl:if test="$i &lt;= $count">
            <xsl:call-template name="for.loop">
                <xsl:with-param name="i" select="$i + 1" />
                <xsl:with-param name="count" select="$count">
                <xsl:with-param name="comment" select="substring-after(//NTE_NotesAndCommentsSegment_2[position() = $i + 1]/NTE_3_Comment, '?')" />
            </xsl:call-template>
        </xsl:if>

    </xsl:template>

    <xsl:template match="/ | @* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
于 2012-06-11T19:16:12.630 に答える
0

(1) の解決策は、次の XPath 式になります。

//NTE_NotesAndCommentsSegment_2/NTE_3_Comment[starts-with(text(), 'Question 1')]
于 2012-06-11T18:08:48.983 に答える