xslt使用xslt从传入的有效负载生成输出

时间:2018-03-30 19:04:12

标签: xslt

我想从传入的有效负载生成响应。

这是我的传入有效载荷:

<Request>
    <PrRequest>
        <RequestWork>
            <QAList>
                <AnswerID>String</AnswerID>
                <QuestionId>066Q</QuestionId>
                <QuestionText>Reason for Visiting </QuestionText>
                <VersionNumber>V1</VersionNumber>
                <Answers>
                    <AnswerText>Rogane X</AnswerText>
                    <AnswerText>Rogane Y</AnswerText>
                </Answers>
            </QAList>
            <QAList>
                <AnswerID>066</AnswerID>
                <QuestionId>066Q</QuestionId>
                <QuestionText>Reason for Visiting </QuestionText>
                <VersionNumber>V1.1</VersionNumber>
                <Answers>
                    <AnswerText>5</AnswerText>
                </Answers>
            </QAList>
        </RequestWork>
    </PrRequest>
</Request>

我的回答应该是:

<Work>
    <Response>
        <AnswerID>String</AnswerID>
        <QuestionId>066Q</QuestionId>
        <QuestionText>Reason for Visiting </QuestionText>
        <Answers>
            <Text> X</Text>
            <Text> Y</Text>
        </Answers>
    </Response>
    <Response>
        <AnswerID>String</AnswerID>
        <QuestionId>066Q</QuestionId>
        <QuestionText>Reason for Visiting </QuestionText>
        <Answers>
            <Text>5</Text>
        </Answers>
    </Response>
</Work>

1 个答案:

答案 0 :(得分:0)

这是一个可以开始的XSLT。

根据输出,假设您必须在答案文本spaceRogane XRogane Y之后获取字符串,建议进行以下更改。如果AnswerText的值有任何变化,则输出会有所不同。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

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

    <xsl:template match="QAList">
        <Response>
            <AnswerID>String</AnswerID>
            <xsl:copy-of select="QuestionId" />
            <xsl:copy-of select="QuestionText" />
            <xsl:apply-templates select="Answers" />
        </Response>
    </xsl:template>

    <xsl:template match="Answers">
        <xsl:copy>
            <xsl:for-each select="AnswerText">
                <Text>
                    <xsl:choose>
                        <xsl:when test="contains(., ' ')">
                            <xsl:value-of select="substring-after(., ' ')" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="." />
                        </xsl:otherwise>
                    </xsl:choose>
                </Text>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出

<Work>
    <Response>
        <AnswerID>String</AnswerID>
        <QuestionId>066Q</QuestionId>
        <QuestionText>Reason for Visiting </QuestionText>
        <Answers>
            <Text>X</Text>
            <Text>Y</Text>
        </Answers>
    </Response>
    <Response>
        <AnswerID>String</AnswerID>
        <QuestionId>066Q</QuestionId>
        <QuestionText>Reason for Visiting </QuestionText>
        <Answers>
            <Text>5</Text>
        </Answers>
    </Response>
</Work>