访问不同xml xpath中的信息,并显示多个相同的路径

时间:2017-02-07 08:49:33

标签: xml xslt

我是XSLT的新手,在从当前的例子中提取我想要的所有信息时遇到了一些麻烦。

在下面的示例中,我感兴趣的还有消息和通知文本属性,正如您所看到的,它也是重复节点。能否请您提取这些信息?

<?xml version="1.0"?><chatTranscript startAt="2016-10-06T09:16:40Z" sessionId="0001GaBYC53D000K">
    <newParty userId="007957F616780001" timeShift="1" visibility="ALL" eventId="1">
        <userInfo personId="" userNick="John Doe" userType="CLIENT" protocolType="FLEX" timeZoneOffset="120"/>
        <userData>
            <item key="GMSServiceId">5954d184-f89d-4f44-8c0f-a772d458b353</item>
            <item key="IdentifyCreateContact">3</item>
            <item key="MediaType">chat</item><item key="TimeZone">120</item>
            <item key="_data_id">139-e9826bf5-c5a4-40e5-a729-2cbdb4776a43</item>
            <item key="firstName">John</item><item key="first_name">John</item>
            <item key="lastName">Doe</item>
            <item key="last_name">Doe</item>
            <item key="location_lat">37.8197</item>
            <item key="location_long">-122.4786</item>
            <item key="userDisplayName">John Doe</item>
        </userData>
    </newParty>

    <message userId="007957F616780001" timeShift="5" visibility="ALL" eventId="2">
        <msgText msgType="text" treatAs="NORMAL">This is message one.</msgText>
    </message>

    <message userId="007957F616780001" timeShift="5" visibility="ALL" eventId="2">
        <msgText msgType="text" treatAs="NORMAL">This is message two.</msgText>
    </message>

    <notice userId="007957F616780001" timeShift="246" visibility="ALL" eventId="3">
        <noticeText noticeType="USER_CUSTOM">This is notice one.</noticeText>
    </notice>

    <notice userId="007957F616780001" timeShift="246" visibility="ALL" eventId="3">
        <noticeText noticeType="USER_CUSTOM">This is notice two.</noticeText>
    </notice>

    <partyLeft userId="007957F616780001" timeShift="291" visibility="ALL" eventId="4" askerId="007957F616780001">
        <reason code="3">left due to disconnect</reason>
    </partyLeft>

</chatTranscript>

和xsl:

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

    <xsl:output method="text"/>

    <xsl:template match="/chatTranscript/newParty[count(*) > 1]">
        <xsl:variable name="curParty" select="@userId" />
            GMSServiceId - <xsl:value-of select="userData/item[@key='GMSServiceId']"/>
            IdentifyCreateContact: <xsl:value-of select="userData/item[@key='IdentifyCreateContact']"/>
            MediaType: <xsl:value-of select="userData/item[@key='MediaType']"/>
            firstName: <xsl:value-of select="userData/item[@key='firstName']"/>
            userDisplayName: <xsl:value-of select="userData/item[@key='userDisplayName']"/>
    </xsl:template>

    <xsl:template match="/chatTranscript/message[count(*) > 1]">
        <xsl:variable name="curParty" select="@userId" />
        Message Text: <xsl:value-of select="msgText"/>
    </xsl:template>

    <xsl:template match="text()" />

</xsl:stylesheet>

目前的输出是:

GMSServiceId - 5954d184-f89d-4f44-8c0f-a772d458b353 IdentifyCreateContact: 3 MediaType: chat firstName: John userDisplayName: John Doe

当然我想将消息文本和通知文本添加到此列表中。

我认为现在抛出的是两个案例中xml的结构不同 - 我不知道如何处理第二种情况(用于消息和通知)。

1 个答案:

答案 0 :(得分:0)

我不确定您期望得到什么,但值得注意的是模板:

<xsl:template match="/chatTranscript/message[count(*) > 1]">

根本没有执行,因为示例中的所有message元素都只有一个子元素。删除谓词,结果将是:

    GMSServiceId - 5954d184-f89d-4f44-8c0f-a772d458b353
    IdentifyCreateContact: 3
    MediaType: chat
    firstName: John
    userDisplayName: John Doe
Message Text: This is message one.
Message Text: This is message two.