我有以下 xml,并根据开始使用的教程修改了一个 xsl 文件。返回的 HTML 包含我在 XSL 中指定的字符串,但未填充 XML 文件中的实际值。
解决方案可能非常简单,但此时我需要有人向我指出这一点。
XML:
<?xml version="1.0"?>
<chatTranscript startAt="2016-11-01T10:40:18Z" sessionId="0001JaC1DVH2001E">
<newParty userId="0079581871120009" timeShift="0" visibility="ALL" eventId="1">
<userInfo personId="" userNick="John" userType="CLIENT" protocolType="FLEX" timeZoneOffset="0"/>
<userData>
<item key="EmailAddress">dskim@gmail.com</item>
<item key="FirstName">John</item>
<item key="IdentifyCreateContact">3</item>
<item key="LastName">Doe</item>
<item key="MediaType">chat</item>
<item key="TimeZone">120</item>
</userData>
</newParty>
<message userId="0079581871120009" timeShift="15" visibility="ALL" eventId="2">
<msgText msgType="text">ehfdfdfd</msgText></message>
<newParty userId="00795818713E000C" timeShift="45" visibility="ALL" eventId="3"><userInfo personId="1" userNick="allendei" userType="AGENT" protocolType="BASIC" timeZoneOffset="120"/>
</newParty><partyLeft userId="00795818713E000C" timeShift="50" visibility="ALL" eventId="4" askerId="00795818713E000C">
<reason code="1">left with request to close if no agents</reason></partyLeft>
<partyLeft userId="0079581871120009" timeShift="50" visibility="ALL" eventId="5" askerId="00795818713E000C">
<reason code="4">removed by other party</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="/">
Email - <xsl:value-of select="/chatTranscript/newParty/userData/item/EmailAddress"/>
Fist Name: <xsl:value-of select="/chatTranscript/newParty/FirstName"/>
Identify Create Contact: <xsl:value-of select="/chatTranscript/newParty/IdentifyCreateContact"/>
Last Name: <xsl:value-of select="/chatTranscript/newParty/LastName"/>
Media Type: <xsl:value-of select="/chatTranscript/newParty/MediaType"/>
Message: <xsl:value-of select="/chatTranscript/message/msgText"/>
Reason for leaving: <xsl:value-of select="/chatTranscript/message/reason"/>
</xsl:template>
<xsl:template match="newParty">
- <xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
我的目标是获取我试图通过 XSL 检索的字段的文本值,然后再检索一些标签属性。
最佳答案
问题出在您的 XPath 上。首先,您缺少 userInfo
步骤,其次您需要一个针对属性的谓词。
例如,对于电子邮件地址:
chatTranscript/newParty/userInfo/userData/item[@key="EmailAddress"]
没有名为“EmailAddress”的节点。这是一个 item
节点,它的 @key 属性值为“EmailAddress”。
您的其他 XPath 也有类似的问题。例如,对于其他人,您不仅可以省略 userInfo
,还可以省略 userData
。
关于javascript - 生成要应用于 XML 文件的 XSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41954541/