我需要XSLT转换,以将包含属性中数据值的XML的一种格式转换为具有相同数据值但CSV格式与“ CDATA”中的文本的XML。
当输入XML是:-
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE labels SYSTEM "label.dtd">
<labels _FORMAT="c:\labels\format\VSAT_RCV_INV_LABEL.btw" _QUANTITY="1" _PRINTERNAME="STOUT" _JOBNAME="LBL1273712">
<label>
<variable name= "current_date">18-OCT-2018</variable>
<variable name= "item">1044103</variable>
<variable name= "item_description">TERM,RING,22-16 AWG,#4,INSL,RED,PB FREE</variable>
<variable name= "locator">INRE</variable>
<variable name= "mfg_part_num"></variable>
</label>
</labels>
输出XML必须为:-
<xml version="1.0"?>
<XMLScript Version="1.0">
<Command>
<Print JobName="LBL1273712">
<PrintSetup>
<IdenticalCopiesOfLabel>1</IdenticalCopiesOfLabel>
<Printer>STOUT</Printer>
</PrintSetup>
<Format>c:\labels\format\vsat_rcv_inv_label.btw</Format>
<RecordSet Name="Text File 1" Type="btTextFile" AddIfNone="true">
<Delimitation>btDelimMixedQuoteAndComma</Delimitation>
<UseFieldNamesFromFirstRecord>true</UseFieldNamesFromFirstRecord>
<TextData>
<![CDATA[
"current_date","item","item_description","locator","mfg_part_num"
"18-OCT-2018", "1044103", "TERM,RING,22-16 AWG,#4,INSL,RED,PB FREE", "INRE", ""
]]>
</TextData>
</RecordSet>
</Print>
</Command>
</XMLScript>
由于输入XML中属性的顺序不断变化,因此我想对CDATA文本中的数据字段名称进行硬编码。
例如,有时输入XML包含
顺序的属性但是我需要在输出XML中保持一个特定的顺序,例如
如何使用XSLT实现呢?
答案 0 :(得分:0)
您可以使用以下XSLT-1.0样式表将输入XML转换为所需的输出XML:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" cdata-section-elements="TextData"/>
<xsl:template match="/labels">
<XMLScript Version="1.0">
<Command>
<Print JobName="{@_JOBNAME}">
<PrintSetup>
<IdenticalCopiesOfLabel><xsl:value-of select="@_QUANTITY" /></IdenticalCopiesOfLabel>
<Printer><xsl:value-of select="@_PRINTERNAME" /></Printer>
</PrintSetup>
<Format><xsl:value-of select="@_FORMAT" /></Format>
<xsl:for-each select="label">
<RecordSet Name="Text File 1" Type="btTextFile" AddIfNone="true">
<Delimitation>btDelimMixedQuoteAndComma</Delimitation>
<UseFieldNamesFromFirstRecord>true</UseFieldNamesFromFirstRecord>
<TextData>
<xsl:value-of select="concat('"','current_date','", "','item','", "','item_description','", "','locator','", "','mfg_part_num','"
')" />
<xsl:value-of select="concat('"',
variable[@name='current_date'],'", "',
variable[@name='item'],'", "',
variable[@name='item_description'],'", "',
variable[@name='locator'],'", "',
variable[@name='mfg_part_num'],'"'
)" />
</TextData>
</RecordSet>
</xsl:for-each>
</Print>
</Command>
</XMLScript>
</xsl:template>
</xsl:stylesheet>
CDATA部分TextData
是通过cdata-section-elements
元素上的xsl:output
实现的。其余只是特定元素和属性的副本。