我需要使用XSLT代码循环这个?

时间:2017-06-22 20:50:08

标签: xml loops xslt foreach

我很难循环使用它。

XML CODE:

    <ProcessData>
    <Messages>
        <MessageIds/>
        <MessageNames>
            <MessageName>tpname1_abc.txt</MessageName>                
            <MessageName>tpname2_def.txt</MessageName>
            <MessageName>tpname3_pqr.txt</MessageName>
            <MessageName>tpname4_xyz.txt</MessageName>
        </MessageNames>
    </Messages>
</ProcessData>

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="text"/>
    <xsl:template match="/">
    Invalid File Received for Enrollments Inbound process in mailbox /sftptps/enrollments/deadletter


                <xsl:for-each select="/ProcessData/Messages/MessageNames/MessageName">
                      Username: <xsl:value-of select="substring-before(/ProcessData/Messages/MessageNames/MessageName/text(),'_')"/>       File Name:<xsl:value-of select="substring-after(/ProcessData/Messages/MessageNames/MessageName/text(),'_')"/>

                        <xsl:text>&#xd;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

到目前为止,这是我的输出:

Invalid File Received for Enrollments Inbound process in mailbox /sftptps/enrollments/deadletter



                      Username: tpname1       File Name:abc.txt
                      Username: tpname1       File Name:abc.txt
                      Username: tpname1       File Name:abc.txt
                      Username: tpname1       File Name:abc.txt

我需要它像这样:

Invalid File Received for Enrollments Inbound process in mailbox /sftptps/enrollments/deadletter



                      Username: tpname1       File Name:abc.txt
                      Username: tpname2       File Name:def.txt
                      Username: tpname3       File Name:pqr.txt
                      Username: tpname4       File Name:xyz.txt

我在这里做错了什么?我确信有一些东西我不想循环它。

1 个答案:

答案 0 :(得分:0)

你已经在那里 - 不要从顶端开始。变化:

<xsl:value-of select="substring-before(/ProcessData/Messages/MessageNames/MessageName/text(),'_')"/>

为:

<xsl:value-of select="substring-before(.,'_')"/>

和:

<xsl:value-of select="substring-after(/ProcessData/Messages/MessageNames/MessageName/text(),'_')"/>

为:

<xsl:value-of select="substring-after(.,'_')"/>

您现在拥有的选择所有 MessageName节点,从文档的根开始,并在XSLT 1.0中返回第一个节点的值。