需要XSLT将具有不同属性值的xml转换为具有CDATA的xml
我需要一个xslt转换,它将转换由一个应用程序生成并发送到另一个应用程序进行处理的xml。下面是一个示例源xml,其中包含数据字段名及其相关数据,如字段名的“当前_日期”、“项目”和“2018年10月18日”、“1044103”等,。。对于数据值需要XSLT将具有不同属性值的xml转换为具有CDATA的xml,xml,xslt,Xml,Xslt,我需要一个xslt转换,它将转换由一个应用程序生成并发送到另一个应用程序进行处理的xml。下面是一个示例源xml,其中包含数据字段名及其相关数据,如字段名的“当前_日期”、“项目”和“2018年10月18日”、“1044103”等,。。对于数据值 <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> <labels _JOBNAME="LBL1273711"> <label
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<labels _JOBNAME="LBL1273711">
<label>
<variable name= "current_date">18-OCT-2018</variable>
<variable name= "item">1044103</variable>
<variable name= "item_description">RING,22-16 AWG,#4,RED,PB FREE</variable>
<variable name= "locator">INRE</variable>
</label>
</labels>
2018年10月18日
1044103
环,22-16 AWG,#4,红色,无铅
印度卢比
将上述xml转换为以下xml:
<XMLScript Version="1.0">
<Command>
<Print JobName="LBL1273711">
<RecordSet Name="Text File 1" Type="btTextFile" AddIfNone="true">
<TextData><![CDATA[
current_date", "item", "item_description", "locator"
"18-OCT-2018", "1044103", "RING,22-16 AWG,#4,RED,PB FREE", "INRE"
]]></TextData>
</RecordSet>
</Print>
</Command>
</XMLScript>
如果您可以假设,对于给定的XML,每个标签
下都有相同的变量
元素,那么您可以这样做来输出标题
<xsl:for-each select="label[1]/variable">
<xsl:if test="position() > 1">,</xsl:if>
<xsl:value-of select="concat('"', @name, '"')" />
</xsl:for-each>
完美的非常感谢你。XSLT2.0代码段的Thx。我使用的应用程序目前没有处理它,但我会保留它并在升级时使用它。
<xsl:for-each select="label[1]/variable">
<xsl:if test="position() > 1">,</xsl:if>
<xsl:value-of select="concat('"', @name, '"')" />
</xsl:for-each>
<xsl:for-each select="variable">
<xsl:if test="position() > 1">,</xsl:if>
<xsl:value-of select="concat('"', ., '"')" />
</xsl:for-each>
<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}">
<RecordSet Name="Text File 1" Type="btTextFile" AddIfNone="true">
<TextData>
<xsl:for-each select="label[1]/variable">
<xsl:if test="position() > 1">,</xsl:if>
<xsl:value-of select="concat('"', @name, '"')" />
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:for-each select="label">
<xsl:for-each select="variable">
<xsl:if test="position() > 1">,</xsl:if>
<xsl:value-of select="concat('"', ., '"')" />
</xsl:for-each>
<xsl:text> </xsl:text>
</xsl:for-each>
</TextData>
</RecordSet>
</Print>
</Command>
</XMLScript>
</xsl:template>
</xsl:stylesheet>
<TextData>
<xsl:value-of select="label[1]/variable/concat('"', @name, '"')" separator="," />
<xsl:text> </xsl:text>
<xsl:for-each select="label">
<xsl:value-of select="variable/concat('"', ., '"')" separator="," />
<xsl:text> </xsl:text>
</xsl:for-each>
</TextData>