首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xsl生成逗号分隔的xml

使用xsl生成逗号分隔的xml
EN

Stack Overflow用户
提问于 2013-06-13 23:47:09
回答 1查看 171关注 0票数 0

这看起来很简单,但我没有生成所需的输出。任何帮助都将不胜感激。

输入XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<Event xmlns="urn:sobject.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Id>00Uc0000001iioEEAQ</Id>
<Subject>TestMeeting2</Subject>
<StartDateTime xsi:nil="true"/>
<Owner>
    <Name>dev0044</Name>
    <Email>testuser0044@gmail.com</Email>
</Owner>
<Notes_Universal_ID__c xsi:nil="true"/>
<Location>San Jose</Location>
<IsPrivate>false</IsPrivate>
<IsDeleted>false</IsDeleted>
<EndDateTime xsi:nil="true"/>
<Description>test description</Description>
<EventRelations>
    <EventRelation>
        <EventId>00Uc0000001iioEEAQ</EventId>
        <RelationId>005c0000000T9YQAA0</RelationId>
        <Relation>
            <Email>testuser0045@gmail.com</Email>
        </Relation>
    </EventRelation>
    <EventRelation>
        <EventId>00Uc0000001iioEEAQ</EventId>
        <RelationId>005c0000000T9YaAAK</RelationId>
        <Relation>
            <Email>testuser0047@gmail.com</Email>
        </Relation>
    </EventRelation>
</EventRelations>

所需的输出XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<CREATEAPPOINTMENT>
<STRUSERNAME>testuser0044</STRUSERNAME>
<STRSTARTDATETIME/>
<STRENDDATETIME/>
<STRSUBJECT>TestMeeting2</STRSUBJECT>
<STRDESCRIPTION>Email : testuser0045@gmail.com, testuser0047@gmail.com. Description:    test description</STRDESCRIPTION>
<SRTLOCATION>San Jose</SRTLOCATION>
<STRPRIVATE>0</STRPRIVATE>
<STRAVAILABLE>0</STRAVAILABLE>
</CREATEAPPOINTMENT>

我可以获取除description字段之外的所有内容。STRDESCRIPTION将包含EventRelation/EventRelation/Relation/ email (逗号分隔的电子邮件地址)和实际描述字段的串联。

下面是我正在尝试使用的xsl:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
xmlns:sf="urn:partner.soap.sforce.com" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/">
<xsl:output indent="yes" method="xml"/>

<xsl:template match="*:Event">
    <xsl:element name="CREATEAPPOINTMENT">
        <xsl:element name="STRUSERNAME"><xsl:value-of select="*:Owner/*:Name"/></xsl:element>
        <xsl:element name="STRSTARTDATETIME"><xsl:value-of select="*:StartDateTime"/></xsl:element>
        <xsl:element name="STRENDDATETIME"><xsl:value-of select="*:EndDateTime"/></xsl:element>
        <xsl:element name="STRSUBJECT"><xsl:value-of select="*:Subject"/></xsl:element>
        <xsl:variable name="description" select="*:Description"/>

        <xsl:element name="SRTDESCRIPTION">
        <xsl:for-each select="*:EventRelations/*:EventRelation">
            <xsl:variable name="email" select="*:Relation/*:Email"/>
                <!-- <xsl:value-of select="concat('Email : ', $email, ' : Description : ')"></xsl:value-of> -->
            <xsl:value-of select="$email"/>
            <xsl:text>, </xsl:text>
            <xsl:value-of select="$description"></xsl:value-of>
            <xsl:text>, </xsl:text>
        </xsl:for-each>
        </xsl:element>

        <xsl:element name="SRTLOCATION"><xsl:value-of select="*:Location"/></xsl:element>

        <xsl:choose>
            <xsl:when test="*:IsPrivate = 'false'">
                <xsl:element name="STRPRIVATE">0</xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="STRPRIVATE">1</xsl:element>
            </xsl:otherwise>
        </xsl:choose>

        <xsl:choose>
            <xsl:when test="*:IsPrivate = 'false'">
                <xsl:element name="STRAVAILABLE">0</xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="STRAVAILABLE">1</xsl:element>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:element>
</xsl:template>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-14 02:26:50

由于使用的是XSLT2.0,因此可以使用xsl:value-ofseparator属性。您也可以使用xpath-default-namespace,这样就不必使用*作为前缀。

示例:

代码语言:javascript
复制
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xpath-default-namespace="urn:sobject.partner.soap.sforce.com">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="/*">
        <CREATEAPPOINTMENT>
            <xsl:apply-templates select="(Owner,StartDateTime,EndDateTime,
                Subject,Description,Location,IsPrivate)"/>
        </CREATEAPPOINTMENT>
    </xsl:template>

    <xsl:template match="Owner/Name">
        <STRUSERNAME><xsl:value-of select="."/></STRUSERNAME>
    </xsl:template>

    <xsl:template match="StartDateTime">
        <STRSTARTDATETIME><xsl:value-of select="."/></STRSTARTDATETIME>
    </xsl:template>

    <xsl:template match="EndDateTime">
        <STRENDDATETIME><xsl:value-of select="."/></STRENDDATETIME>
    </xsl:template>

    <xsl:template match="Subject">
        <STRSUBJECT><xsl:value-of select="."/></STRSUBJECT>
    </xsl:template>

    <xsl:template match="Description">
        <STRDESCRIPTION>
            <xsl:text>Email: </xsl:text>
            <xsl:value-of select="../EventRelations/*/Relation/Email" separator=", "/>
            <xsl:text> DESCRIPTION: </xsl:text>
            <xsl:value-of select="."/>
        </STRDESCRIPTION>
    </xsl:template>

    <xsl:template match="Location">
        <SRTLOCATION><xsl:value-of select="."/></SRTLOCATION>
    </xsl:template>

    <xsl:template match="IsPrivate">
        <STRPRIVATE><xsl:apply-templates/></STRPRIVATE>
        <STRAVAILABLE><xsl:apply-templates/></STRAVAILABLE>
    </xsl:template>

    <xsl:template match="IsPrivate/text()">
        <xsl:choose>
            <xsl:when test=".='false'"><xsl:text>0</xsl:text></xsl:when>
            <xsl:otherwise><xsl:text>1</xsl:text></xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

输出

代码语言:javascript
复制
<CREATEAPPOINTMENT>
   <STRUSERNAME>dev0044</STRUSERNAME>
   <STRSTARTDATETIME/>
   <STRENDDATETIME/>
   <STRSUBJECT>TestMeeting2</STRSUBJECT>
   <STRDESCRIPTION>Email: testuser0045@gmail.com, testuser0047@gmail.com DESCRIPTION: test description</STRDESCRIPTION>
   <SRTLOCATION>San Jose</SRTLOCATION>
   <STRPRIVATE>0</STRPRIVATE>
   <STRAVAILABLE>0</STRAVAILABLE>
</CREATEAPPOINTMENT>

注意:元素的顺序与问题中的输出不同。如果顺序很重要,您可以在指定顺序的/*模板的xsl:apply-templates中添加一个select。(如果有帮助,我可以修改它。)

此外,如果您通过在开头添加"STR“来创建新元素,则可以将StartDateTimeEndDateTimeSubjectLocation的模板替换为以下模板:

代码语言:javascript
复制
<xsl:template match="StartDateTime|EndDateTime|Subject|Location">
    <xsl:element name="STR{upper-case(local-name())}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17091441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档