0

我对 XML(销售订单)有疑问。我导入具有 XML 结构的文件 (csv)。我使用的工具自动从这个文件创建一个具有相同结构的 XML。所有标签都在同一级别。

<?xml version="1.0" encoding="UTF-8"?>
<ORDER>
<ENV>
    <SenderCode>1234</SenderCode>
    <Test>N</Test>
</ENV>
<HDR>
    <OrderNumber>100</OrderNumber>
    <OrderDate>201206080000</OrderDate>
</HDR>
<ROW>
    <LineNumber>1</LineNumber>
    <Article>A</Article>
</ROW>
<ROW>
    <LineNumber>2</LineNumber>
    <Article>B</Article>
</ROW>
<ROW>
    <LineNumber>3</LineNumber>
    <Article>C</Article>
</ROW>
<ROW>
    <LineNumber>4</LineNumber>
    <Article>D</Article>
</ROW>
<HDR>
    <OrderNumber>101</OrderNumber>
    <OrderDate>201206080000</OrderDate>
</HDR>
<ROW>
    <LineNumber>1</LineNumber>
    <Article>E</Article>
</ROW>
<ROW>
    <LineNumber>2</LineNumber>
    <Article>F</Article>
</ROW>
</ORDER>

我需要的输出是:

<?xml version="1.0" encoding="UTF-8"?>
<ORDER>
  <ENV>
    <SenderCode>1234</SenderCode>
    <Test>N</Test>
  </ENV>
  <HDR>
    <OrderNumber>100</OrderNumber>
    <OrderDate>201206080000</OrderDate>
    <ROW>
      <LineNumber>1</LineNumber>
      <Article>A</Article>
    </ROW>
    <ROW>
      <LineNumber>2</LineNumber>
      <Article>B</Article>
    </ROW>
    <ROW>
      <LineNumber>3</LineNumber>
      <Article>C</Article>
    </ROW>
    <ROW>
      <LineNumber>4</LineNumber>
      <Article>D</Article>
    </ROW>
  </HDR>
  <ENV>
    <SenderCode>1234</SenderCode>
    <Test>N</Test>
  </ENV>
  <HDR>
    <OrderNumber>101</OrderNumber>
    <OrderDate>201206080000</OrderDate>
    <ROW>
      <LineNumber>1</LineNumber>
      <Article>E</Article>
    </ROW>
    <ROW>
      <LineNumber>2</LineNumber>
      <Article>F</Article>
    </ROW>
  </HDR>
</ORDER>

我怎么能用 XSLT 做到这一点?当我尝试这个时,所有六个ROW标签都在HDR(1)和HDR(2)下。

<?xml version="1.0" encoding="UTF-8"?>
<ORDER>
<ENV>
    <SenderCode>1234</SenderCode>
    <Test>N</Test>
</ENV>
<HDR>
    <OrderNumber>100</OrderNumber>
    <OrderDate>201206080000</OrderDate>
    <ROW>
        <LineNumber>1</LineNumber>
        <Article>A</Article>
    </ROW>
    <ROW>
        <LineNumber>2</LineNumber>
        <Article>B</Article>
    </ROW>
    <ROW>
        <LineNumber>3</LineNumber>
        <Article>C</Article>
    </ROW>
    <ROW>
        <LineNumber>4</LineNumber>
        <Article>D</Article>
    </ROW>
    <ROW>
        <LineNumber>1</LineNumber>
        <Article>E</Article>
    </ROW>
    <ROW>
        <LineNumber>2</LineNumber>
        <Article>F</Article>
    </ROW>
</HDR>
<ENV>
    <SenderCode>1234</SenderCode>
    <Test>N</Test>
</ENV>
<HDR>
    <OrderNumber>101</OrderNumber>
    <OrderDate>201206080000</OrderDate>
    <ROW>
        <LineNumber>1</LineNumber>
        <Article>A</Article>
    </ROW>
    <ROW>
        <LineNumber>2</LineNumber>
        <Article>B</Article>
    </ROW>
    <ROW>
        <LineNumber>3</LineNumber>
        <Article>C</Article>
    </ROW>
    <ROW>
        <LineNumber>4</LineNumber>
        <Article>D</Article>
    </ROW>
    <ROW>
        <LineNumber>1</LineNumber>
        <Article>E</Article>
    </ROW>
    <ROW>
        <LineNumber>2</LineNumber>
        <Article>F</Article>
    </ROW>
</HDR>
</ORDER>

有人可以帮助我吗?

谢谢

4

1 回答 1

1

此转换将按照您的要求进行。它使用一个键来识别ROW每个紧随其后的所有元素HDR

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

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes" />

  <xsl:key name="row-by-hdr-id" match="ROW" use="generate-id(preceding-sibling::HDR[1])"/>

  <xsl:template match="node()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/ORDER">
    <xsl:copy>
      <xsl:apply-templates select="HDR"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="HDR">
    <xsl:apply-templates select="preceding-sibling::ENV"/>
    <xsl:copy>
      <xsl:apply-templates/>
      <xsl:apply-templates select="key('row-by-hdr-id', generate-id())"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

输出

<?xml version="1.0" encoding="utf-8"?>
<ORDER>
   <ENV>
      <SenderCode>1234</SenderCode>
      <Test>N</Test>
   </ENV>
   <HDR>
      <OrderNumber>100</OrderNumber>
      <OrderDate>201206080000</OrderDate>
      <ROW>
         <LineNumber>1</LineNumber>
         <Article>A</Article>
      </ROW>
      <ROW>
         <LineNumber>2</LineNumber>
         <Article>B</Article>
      </ROW>
      <ROW>
         <LineNumber>3</LineNumber>
         <Article>C</Article>
      </ROW>
      <ROW>
         <LineNumber>4</LineNumber>
         <Article>D</Article>
      </ROW>
   </HDR>
   <ENV>
      <SenderCode>1234</SenderCode>
      <Test>N</Test>
   </ENV>
   <HDR>
      <OrderNumber>101</OrderNumber>
      <OrderDate>201206080000</OrderDate>
      <ROW>
         <LineNumber>1</LineNumber>
         <Article>E</Article>
      </ROW>
      <ROW>
         <LineNumber>2</LineNumber>
         <Article>F</Article>
      </ROW>
   </HDR>
</ORDER>
于 2013-05-21T09:48:59.877 回答