简体   繁体   English

需要XSLT将具有不同属性值的xml转换为具有CDATA的xml

[英]Need XSLT to transform xml having varying attribute values to xml with CDATA

I need an xslt transform that would convert an xml generated by one aplication and sent to another application to be processed. 我需要一个xslt转换,它将转换一个应用程序生成的xml并发送到另一个要处理的应用程序。 Below is a sample source xml containing data field names and their relevant data like 'current_date','item'..for field names and '18-OCT-2018','1044103',.. for data values. 以下是一个示例源xml,其中包含数据字段名称及其相关数据,例如'current_date','item'..用于字段名称,'18 -OCT-2018','1044103'..用于数据值。

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

The above xml is to transformed as the below xml: 上面的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>

The data field names, the field count and their values would vary and change from one incoming xml to another.I'm using the below xslt in one requirement where the field names and the field count are hardcoded. 数据字段名称,字段计数及其值会有所不同,并且从一个传入的xml更改为另一个。我在一个要求中使用下面的xslt,其中字段名称和字段计数是硬编码的。 But I need it to be changed to transform the source xml of any number of field count and field names provided in the variable/name. 但是我需要对其进行更改,以转换变量/名称中提供的任意数量的字段计数和字段名称的源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}">
            <RecordSet Name="Text File 1" Type="btTextFile" AddIfNone="true">
                <TextData>
                    <xsl:value-of select="concat('&#xa;'
                        ,'current_date','&quot;, &quot;','item','&quot;, &quot;',
                        'item_description','&quot;, &quot;','locator','&quot;&#xa;')" />
                    <xsl:for-each select="label">
                        <xsl:value-of select="concat('&quot;',
                        variable[@name='current_date'],'&quot;, &quot;',
                        variable[@name='item'],'&quot;, &quot;',
                        variable[@name='item_description'],'&quot;, &quot;',
                        variable[@name='locator'],'&quot;&#xa;'
                        )" />
            </xsl:for-each>
                    </TextData>
                </RecordSet>
        </Print>
    </Command>
    </XMLScript>
    </xsl:template>
</xsl:stylesheet>

Thanks in advance. 提前致谢。

If you can assume, for a given XML, each label will have the same variable elements under it, you can do this to output the header.... 如果可以假设,对于给定的XML,每个label都有相同的variable元素,则可以执行此操作以输出标头。

<xsl:for-each select="label[1]/variable">
   <xsl:if test="position() > 1">,</xsl:if>
  <xsl:value-of select="concat('&quot;', @name, '&quot;')" />
</xsl:for-each>

And similarly, for each label, do this to output the values 同样,对于每个标签,执行此操作以输出值

<xsl:for-each select="variable">
  <xsl:if test="position() > 1">,</xsl:if>
  <xsl:value-of select="concat('&quot;', ., '&quot;')" />
</xsl:for-each>

Try this XSLT 试试这个XSLT

<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('&quot;', @name, '&quot;')" />
          </xsl:for-each>
          <xsl:text>&#10;</xsl:text>
          <xsl:for-each select="label">
            <xsl:for-each select="variable">
              <xsl:if test="position() > 1">,</xsl:if>
              <xsl:value-of select="concat('&quot;', ., '&quot;')" />
            </xsl:for-each>
            <xsl:text>&#10;</xsl:text>
          </xsl:for-each>
        </TextData>
      </RecordSet>
    </Print>
  </Command>
</XMLScript>
</xsl:template>
</xsl:stylesheet>

Note, if you can use XSLT 2.0, you can replace the xsl:for-each with simpler xsl:value-of statements... 请注意,如果可以使用XSLT 2.0,则可以用更简单的xsl:value-of语句替换xsl:for-each ...

<TextData>
  <xsl:value-of select="label[1]/variable/concat('&quot;', @name, '&quot;')" separator="," />
  <xsl:text>&#10;</xsl:text>
  <xsl:for-each select="label">
    <xsl:value-of select="variable/concat('&quot;', ., '&quot;')" separator="," />
    <xsl:text>&#10;</xsl:text>
  </xsl:for-each>
</TextData>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM