如何构建样式表以与XSLTPROC一起使用以将XML转换为CSV



需要使用 bash shell 将 xml 文件转换为 csv。 选中我们在 Linux 主机上有可用的 XSLTPROC。 如果还有其他方法可以通过 Bash shell 将其转换为 CSV。 请让我知道。XML文件如下:

<?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>

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

 <?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>

ns1:NumPagesAvailable,ns1:ReasonCode,ns1:ReasonDescription, ns1:lastModified,ns1:id,ns1:cLIName,ns1:cLINumber,ns1:buttonNumber,ns1:callType,ns1:callUsage,ns1:destination,ns1:deviceChannel,ns1:deviceChannelType,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
10,REQ_SUCCESS,Fetch Communication History Successful,2019-03-18T00:00:10+00:00:00,290805235,,,1,resource,none,

如果不进行测试,有两件事会立即脱颖而出:

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

最新更新