生成要应用于XML文件的XSL

时间:2017-01-31 09:59:31

标签: javascript css xml xslt

我有以下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检索的字段的文本值,然后再检索一些标记属性。

2 个答案:

答案 0 :(得分:1)

问题在于您的XPath。首先,您错过了userInfo步骤,其次您需要一个定位属性的谓词。

示例,电子邮件地址:

chatTranscript/newParty/userInfo/userData/item[@key="EmailAddress"]

没有名为“EmailAddress”的节点。有什么,是item节点,其“EmailAddress”作为其@key属性的值。

您的其他XPath也有类似的问题。例如,对于其他人,您不仅省略了userInfo,还省略了userData

答案 1 :(得分:0)

您的XPath axes存在严重问题。对元素的属性使用谓词([...])将极大地改善您的代码。

<?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" />
    Email - <xsl:value-of select="userData/item[@key='EmailAddress']"/>
    Fist Name: <xsl:value-of select="userData/item[@key='FirstName']"/>
    Identify Create Contact: <xsl:value-of select="userData/item[@key='IdentifyCreateContact']"/>
    Last Name: <xsl:value-of select="userData/item[@key='LastName']"/>
    Media Type: <xsl:value-of select="userData/item[@key='MediaType']"/>
    Message: <xsl:value-of select="../message[@userId=$curParty]/msgText/text()"/>
    Reason for leaving: <xsl:value-of select="normalize-space(../partyLeft[@userId=$curParty])" />
  </xsl:template>

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

</xsl:stylesheet>

<强>输出:

Email - dskim@gmail.com
Fist Name: John
Identify Create Contact: 3
Last Name: Doe
Media Type: chat
Message: ehfdfdfd
Reason for leaving: removed by other party