XML-非规范化重复的兄弟姐妹

时间:2014-07-24 22:14:13

标签: xml xslt

我们将目标文件发送给供应商,它们可以包含0-30个可选字段。我们的XSD可以转移到标准化数据库,但它对于低技术打印供应商来说并不是那么好。因此,我想使用XSLT创建数据的非规范化视图,即使是基本用户也可以使用Excel解码。诀窍并不是每个记录都有相同或相同数量的可选字段,我也不知道如何使其与工作保持一致。

<target>
        <transactionNumber>536880912</transactionNumber>
       <optionalField>
            <optionalFieldLabel>PROMO_CODE</optionalFieldLabel>
            <optionalFieldDescription>ABC127</optionalFieldDescription>
        </optionalField>
        <optionalField>
            <optionalFieldLabel>Version</optionalFieldLabel>
            <optionalFieldDescription>CON</optionalFieldDescription>
        </optionalField>
        <optionalField>
            <optionalFieldLabel>cell</optionalFieldLabel>
            <optionalFieldDescription>2</optionalFieldDescription>
        </optionalField>
  </target>
  <target>
        <transactionNumber>536880924</transactionNumber>
        <optionalField>
            <optionalFieldLabel>PROMO_CODE</optionalFieldLabel>
            <optionalFieldDescription>ABC131</optionalFieldDescription>
        </optionalField>
        <optionalField>
            <optionalFieldLabel>Version</optionalFieldLabel>
            <optionalFieldDescription>CON</optionalFieldDescription>
        </optionalField>
  </target>
<target>
        <transactionNumber>536880213</transactionNumber>
        <optionalField>
            <optionalFieldLabel>Version</optionalFieldLabel>
            <optionalFieldDescription>CON</optionalFieldDescription>
        </optionalField>
        <optionalField>
            <optionalFieldLabel>cell</optionalFieldLabel>
            <optionalFieldDescription>1</optionalFieldDescription>
        </optionalField>
  </target>

我是新来的,所以没有图片:(

我正在尝试这样的事情:

536880912 ___ PROMO_CODE ___ ABC127 ___版___ CON ___细胞___ 2

536880924 ___ PROMO_CODE___ABC131___Version___CON

536880213 _____________________________版___ CON ___细胞___ 1

或HTML表格,其中我的optionalfieldlabel是每列的标题。

我刚刚意识到如何简化我的问题。 基本上我只需要删除optionfield和optionalfieldlabel标签,并使用可选的fieldlabel值用新标签替换它们。所以它转换成这样:

<target>
        <transactionNumber>536880912</transactionNumber>
       <PROMO_CODE>
            <optionalFieldDescription>ABC127</optionalFieldDescription>
        </PROMO_CODE>
        <Version>
            <optionalFieldDescription>CON</optionalFieldDescription>
        </Version>
        <cell>
            <optionalFieldDescription>2</optionalFieldDescription>
        </cell>
  </target>
  <target>
        <transactionNumber>536880924</transactionNumber>
        <PROMO_CODE>
            <optionalFieldDescription>ABC131</optionalFieldDescription>
        </PROMO_CODE>
        <Version>
            <optionalFieldDescription>CON</optionalFieldDescription>
        </Version>
  </target>
<target>
        <transactionNumber>536880912</transactionNumber>
        <Version>
            <optionalFieldDescription>CON</optionalFieldDescription>
        </Version>
        <cell>
            <optionalFieldDescription>1</optionalFieldDescription>
        </cell>
  </target>

提前致谢。

2 个答案:

答案 0 :(得分:0)

请参阅下面的样式表。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:strip-space elements="*"/>

    <xsl:output indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <table>
            <tbody>
                <xsl:apply-templates select="root/target"/>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template match="target">
        <tr>
            <td><xsl:value-of select="transactionNumber"/></td>
            <td><xsl:value-of select="optionalField/optionalFieldLabel[.='PROMO_CODE']"/></td>
            <td><xsl:value-of select="optionalField/optionalFieldLabel[.='PROMO_CODE']/following-sibling::optionalFieldDescription"/></td>
            <td><xsl:value-of select="optionalField/optionalFieldLabel[.='Version']"/></td>
            <td><xsl:value-of select="optionalField/optionalFieldLabel[.='Version']/following-sibling::optionalFieldDescription"/></td>
            <td><xsl:value-of select="optionalField/optionalFieldLabel[.='cell']"/></td>
            <td><xsl:value-of select="optionalField/optionalFieldLabel[.='cell']/following-sibling::optionalFieldDescription"/></td>
        </tr>
    </xsl:template>

</xsl:stylesheet>

我想这将是你走得更远的起点。您必须确切地知道完整的30个字段以及放置它们的位置。

答案 1 :(得分:0)

  

基本上我只需要删除选项字段和   optionalfieldlabel标签并使用带有新标签的替换它们   可选的fieldlabel值。

使用身份转换模板按原样复制所有内容,除了由专用模板处理的optionalField元素外,这是相当简单的事情:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="optionalField">
    <xsl:element name="{optionalFieldLabel}">
        <xsl:apply-templates select="optionalFieldDescription"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

应用于更正的输入时:

<root>
   <target>
      <transactionNumber>536880912</transactionNumber>
      <optionalField>
         <optionalFieldLabel>PROMO_CODE</optionalFieldLabel>
         <optionalFieldDescription>ABC127</optionalFieldDescription>
      </optionalField>
      <optionalField>
         <optionalFieldLabel>Version</optionalFieldLabel>
         <optionalFieldDescription>CON</optionalFieldDescription>
      </optionalField>
      <optionalField>
         <optionalFieldLabel>cell</optionalFieldLabel>
         <optionalFieldDescription>2</optionalFieldDescription>
      </optionalField>
   </target>
   <target>
      <transactionNumber>536880924</transactionNumber>
      <optionalField>
         <optionalFieldLabel>PROMO_CODE</optionalFieldLabel>
         <optionalFieldDescription>ABC131</optionalFieldDescription>
      </optionalField>
      <optionalField>
         <optionalFieldLabel>Version</optionalFieldLabel>
         <optionalFieldDescription>CON</optionalFieldDescription>
      </optionalField>
   </target>
   <target>
      <transactionNumber>536880213</transactionNumber>
      <optionalField>
         <optionalFieldLabel>Version</optionalFieldLabel>
         <optionalFieldDescription>CON</optionalFieldDescription>
      </optionalField>
      <optionalField>
         <optionalFieldLabel>cell</optionalFieldLabel>
         <optionalFieldDescription>1</optionalFieldDescription>
      </optionalField>
   </target>
</root>

结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <target>
      <transactionNumber>536880912</transactionNumber>
      <PROMO_CODE>
         <optionalFieldDescription>ABC127</optionalFieldDescription>
      </PROMO_CODE>
      <Version>
         <optionalFieldDescription>CON</optionalFieldDescription>
      </Version>
      <cell>
         <optionalFieldDescription>2</optionalFieldDescription>
      </cell>
   </target>
   <target>
      <transactionNumber>536880924</transactionNumber>
      <PROMO_CODE>
         <optionalFieldDescription>ABC131</optionalFieldDescription>
      </PROMO_CODE>
      <Version>
         <optionalFieldDescription>CON</optionalFieldDescription>
      </Version>
   </target>
   <target>
      <transactionNumber>536880213</transactionNumber>
      <Version>
         <optionalFieldDescription>CON</optionalFieldDescription>
      </Version>
      <cell>
         <optionalFieldDescription>1</optionalFieldDescription>
      </cell>
   </target>
</root>