如何构建样式表以与xsltproc一起使用以将xml转换为csv

时间:2019-04-03 03:00:22

标签: xml linux bash csv xslt

需要使用bash shell将xml文件转换为csv。检查我们在Linux主机上是否有xsltproc。是否还有其他方法可以通过bash shell将其转换为csv。请告诉我。 xml文件如下:

InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest
{
    TemplateId = templateId,
    ObjectId = contactId,
    ObjectType = "contact"
};
InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);

Entity template = instTemplateResp.EntityCollection.Entities[0];

我尝试构建XSL样式表,但无济于事。以下是我尝试过的样式表的详细信息。根目录位于ns1:CommunicationHistoryResponse。 可以为每个基础记录重复ns1:NumPagesAvailable,ns1:ReasonCode,ns1:ReasonDescription。

<?xml version="1.0' encoding="utf-8" standalone="yes"?>
<ns1:CommunicationHistoryResponse xmlns:nsl="http://www.itc.com/bd" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:NumPagesAvailable>10</ns1:NumPagesAvailable>
<ns1:Reason>
  <ns1:ReasonCode>REQ_SUCCESS</ns1:ReasonCode>
<ns1:ReasonDescription>Fetch Communication History 
Successful</ns1:ReasonDescription>
</ns1:Reason>
<ns1:CommunicationHistoryList>
<ns1:CommunicationHistory>
 <ns1:lastModified>2019-03-18T00:00:10+00:00:00</ns1:lastModified>
 <ns1:id>290805235</ns1:id>
 <ns1:cLIName>null</ns1:cLIName>
 <ns1:cLINumber>null</ns1:cLINumber>
 <ns1:buttonNumber>1</ns1:buttonNumber>
 <ns1:callType>resource</ns1:callType>
 <ns1:callUsage>none</ns1:callUsage>
 <ns1:destination>null</ns1:destination>
 <ns1:deviceChannel>7</ns1:deviceChannel>
 <ns1:deviceChannelType>Speaker</ns1:deviceChannelType>
 <ns1:deviceIdId>268435490</ns1:deviceIdId>
 <ns1:displayInCallHistory>true</ns1:displayInCallHistory>
 <ns1:duration>27</ns1:duration>
 <ns1:e164Destination xsi:nill="true" />
 <ns1:eventType>Initiated</ns1:eventType>
 <ns1:parentUserCDIId>369098755</ns1:parentUserCDIId>
 <ns1:personalPointofContactId>0</ns1:personalPointofContactId>
 <ns1:pointofContactId>0</ns1:pointofContactId>
 <ns1:priority>none</ns1:priority>
 <ns1:reasonForDisconnect xsi:nil="true" />
 <ns1:resourceAORId>33563751</ns1:resourceAORId>
 <ns1:rolloverAppearance>1</ns1:rolloverAppearance>
 <ns1:routedDestination></ns1:routedDestination>
 <ns1:schemaDifference_blob_reserved xsi:nil="true" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
 <ns1:schemaDifference_reserved>{}</ns1:schemaDifference_reserved>
 <ns1:startTime>2019-03-17T23:59:43+00:00</ns1:startTime>
 <ns1:trunkBchannel>-1</ns1:trunkBchannel>
 <ns1:trunkId>335543181</ns1:trunkId>
 <ns1:userId>33454412</ns1:userid>
 </ns1:CommunicationHistory>
  <ns1:CommunicationHistory>
 <ns1:lastModified>2019-02-18T00:00:10+00:00:00</ns1:lastModified>
 <ns1:id>290805245</ns1:id>
 <ns1:cLIName>null</ns1:cLIName>
 <ns1:cLINumber>null</ns1:cLINumber>
 <ns1:buttonNumber>2</ns1:buttonNumber>
 <ns1:callType>resource</ns1:callType>
 <ns1:callUsage>none</ns1:callUsage>
 <ns1:destination>null</ns1:destination>
 <ns1:deviceChannel>7</ns1:deviceChannel>
 <ns1:deviceChannelType>Speaker</ns1:deviceChannelType>
 <ns1:deviceIdId>268436230</ns1:deviceIdId>
 <ns1:displayInCallHistory>true</ns1:displayInCallHistory>
 <ns1:duration>26</ns1:duration>
 <ns1:e164Destination xsi:nill="true" />
 <ns1:eventType>Initiated</ns1:eventType>
 <ns1:parentUserCDIId>369198755</ns1:parentUserCDIId>
 <ns1:personalPointofContactId>0</ns1:personalPointofContactId>
 <ns1:pointofContactId>0</ns1:pointofContactId>
 <ns1:priority>none</ns1:priority>
 <ns1:reasonForDisconnect xsi:nil="true" />
 <ns1:resourceAORId>33552751</ns1:resourceAORId>
 <ns1:rolloverAppearance>1</ns1:rolloverAppearance>
 <ns1:routedDestination></ns1:routedDestination>
 <ns1:schemaDifference_blob_reserved xsi:nil="true" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
 <ns1:schemaDifference_reserved>{}</ns1:schemaDifference_reserved>
 <ns1:startTime>2019-02-17T23:59:43+00:00</ns1:startTime>
 <ns1:trunkBchannel>-1</ns1:trunkBchannel>
 <ns1:trunkId>335543191</ns1:trunkId>
 <ns1:userId>33454413</ns1:userid>
 </ns1:CommunicationHistory>
 </ns1:CommunicationHistoryList>
 </nsl:CommunicationHistoryResponse>

 <?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="/root"> ns1:lastModified; 
 ns1:id;ns1:cLIName;ns1:cLINumber;ns1:buttonNumber;ns1:callType;
 ns1:callUsage;ns1:destination;ns1:deviceChannel;
 ns1:deviceChannelTypeSpeaker;
 ns1:deviceIdId;
 ns1:displayInCallHistory;
 ns1:duration;
 ns1:e164Destination;
 ns1:eventType;
 ns1:parentUserCDIId;
 ns1:personalPointofContactId;
 ns1:pointofContactId;
 ns1:priority;
 ns1:reasonForDisconnect;
 ns1:resourceAORId;
 ns1:rolloverAppearance;
 ns1:routedDestination;
 ns1:schemaDifference_blob_reserved;
 ns1:schemaDifference_reserved;
 ns1:startTime;
 ns1:trunkBchannel;
 ns1:trunkId;
 ns1:userId
 <xsl:for-each select="record">
 <xsl:for-each select="data"><xsl:value-of select="ns1:lastModified"/>;
 <xsl:value-of select="ns1:id"/>"/>;
 <xsl:value-of select="ns1:cLIName"/>"/>;
 <xsl:value-of select="ns1:cLINumber"/>;
 <xsl:value-of select="ns1:buttonNumber"/>;
 <xsl:value-of select="ns1:callType"/>;
 <xsl:value-of select="ns1:callUsage"/>;
 <xsl:value-of select="ns1:destination"/>;
 <xsl:value-of select="ns1:deviceChannel"/>;
 <xsl:value-of select="ns1:deviceChannelTypeSpeaker"/>;
 <xsl:value-of select="ns1:deviceIdId"/>;
 <xsl:value-of select="ns1:displayInCallHistory"/>;
 <xsl:value-of select="ns1:duration"/>;
 <xsl:value-of select="ns1:e164Destination"/>;
 <xsl:value-of select="ns1:eventType"/>;
 <xsl:value-of select="ns1:parentUserCDIId"/>;
 <xsl:value-of select="ns1:personalPointofContactId"/>;
 <xsl:value-of select="ns1:pointofContactId"/>;
 <xsl:value-of select="ns1:priority"/>;
 <xsl:value-of select="ns1:reasonForDisconnect"/>;
 <xsl:value-of select="ns1:resourceAORId"/>;
 <xsl:value-of select="ns1:rolloverAppearance"/>;
 <xsl:value-of select="ns1:routedDestination"/>;
 <xsl:value-of select="ns1:schemaDifference_blob_reserved"/>;
 <xsl:value-of select="ns1:schemaDifference_reserved"/>;
 <xsl:value-of select="ns1:startTime"/>;
 <xsl:value-of select="ns1:trunkBchannel"/>;
 <xsl:value-of select="ns1:trunkId"/>;
 <xsl:value-of select="ns1:userId"/>
 <xsl:text>&#xa;</xsl:text></xsl:for-each>
 </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

未经测试,两件事立即突出:

  1. 您的模板匹配/root-但是您的输入没有这样的元素; 根元素的名称为nsl:CommunicationHistoryResponse;
  2. 您正在使用前缀ns1,而没有先将其绑定到命名空间。