0

我当前的 XML 如下所示:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Rowsets CachedTime="" DateCreated="2013-07-30T00:13:32" EndDate="2013-07-30T00:13:29" StartDate="2013-07-29T23:13:29" Version="12.2.4 Build(92)">
    <Rowset>
        <Columns>
            <Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="-9" SourceColumn="Name"/>
            <Column Description="Value" MaxRange="1" MinRange="0" Name="Value" SQLDataType="-9" SourceColumn="Value"/>
            <Column Description="Min" MaxRange="1" MinRange="0" Name="Min" SQLDataType="2" SourceColumn="Min"/>
            <Column Description="Max" MaxRange="1" MinRange="0" Name="Max" SQLDataType="2" SourceColumn="Max"/>
            <Column Description="ObjectDisplay" MaxRange="1" MinRange="0" Name="ObjectDisplay" SQLDataType="12" SourceColumn="ObjectDisplay"/>
            <Column Description="Datatype" MaxRange="1" MinRange="0" Name="Datatype" SQLDataType="-9" SourceColumn="Datatype"/>
        </Columns>
        <Row>
            <Name>Tank</Name>
            <Value>T111</Value>
            <Min>1</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Material</Name>
            <Value>111-Bulk</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Batch</Name>
            <Value>1111111</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
    </Rowset>
    <Rowset>
        <Columns>
            <Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="-9" SourceColumn="Name"/>
            <Column Description="Value" MaxRange="1" MinRange="0" Name="Value" SQLDataType="-9" SourceColumn="Value"/>
            <Column Description="Min" MaxRange="1" MinRange="0" Name="Min" SQLDataType="2" SourceColumn="Min"/>
            <Column Description="Max" MaxRange="1" MinRange="0" Name="Max" SQLDataType="2" SourceColumn="Max"/>
            <Column Description="ObjectDisplay" MaxRange="1" MinRange="0" Name="ObjectDisplay" SQLDataType="12" SourceColumn="ObjectDisplay"/>
            <Column Description="Datatype" MaxRange="1" MinRange="0" Name="Datatype" SQLDataType="-9" SourceColumn="Datatype"/>
        </Columns>
        <Row>
            <Name>Tank</Name>
            <Value>T222</Value>
            <Min>1</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Material</Name>
            <Value>222-Bulk</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Batch</Name>
            <Value>2222222</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
    </Rowset>
    <Rowset>
        <Columns>
            <Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="-9" SourceColumn="Name"/>
            <Column Description="Value" MaxRange="1" MinRange="0" Name="Value" SQLDataType="-9" SourceColumn="Value"/>
            <Column Description="Min" MaxRange="1" MinRange="0" Name="Min" SQLDataType="2" SourceColumn="Min"/>
            <Column Description="Max" MaxRange="1" MinRange="0" Name="Max" SQLDataType="2" SourceColumn="Max"/>
            <Column Description="ObjectDisplay" MaxRange="1" MinRange="0" Name="ObjectDisplay" SQLDataType="12" SourceColumn="ObjectDisplay"/>
            <Column Description="Datatype" MaxRange="1" MinRange="0" Name="Datatype" SQLDataType="-9" SourceColumn="Datatype"/>
        </Columns>
        <Row>
            <Name>Tank</Name>
            <Value>T333</Value>
            <Min>1</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Material</Name>
            <Value>333-Bulk</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Batch</Name>
            <Value>3333333</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
    </Rowset>
</Rowsets>

现在我想要这样的结果 XML:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Rowsets DateCreated="2013-09-11T06:30:41" EndDate="2013-09-03T16:48:47" StartDate="2013-09-03T16:48:47" Version="12.2.4 Build(92)">
    <Rowset>
        <Columns>
            <Column Description="" MaxRange="1" MinRange="0" Name="Tank" SQLDataType="1" SourceColumn="Tank"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="Material" SQLDataType="1" SourceColumn="Material"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="Batch" SQLDataType="1" SourceColumn="Batch"/>
        </Columns>
        <Row>
            <Tank>T111</Tank>
            <Material>111-Bulk</Material>
            <Batch>1111111</Batch>
        </Row>
        <Row>
            <Tank>T222</Tank>
            <Material>222-Bulk</Material>
            <Batch>2222222</Batch>
        </Row>
        <Row>
            <Tank>T333</Tank>
            <Material>333-Bulk</Material>
            <Batch>3333333</Batch>
        </Row>
    </Rowset>
</Rowsets>

我尝试了各种排列和组合,但不知何故我无法获得正确的输出。谁能帮我解决这个问题。

4

1 回答 1

1

Rowset/Row的 's 必须是一致的(Row每个Rowset和相同Row/Name的 's 的数量相同),但你可以尝试这样的事情......

XML 输入

<Rowsets CachedTime="" DateCreated="2013-07-30T00:13:32" EndDate="2013-07-30T00:13:29" StartDate="2013-07-29T23:13:29" Version="12.2.4 Build(92)">
    <Rowset>
        <Columns>
            <Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="-9" SourceColumn="Name"/>
            <Column Description="Value" MaxRange="1" MinRange="0" Name="Value" SQLDataType="-9" SourceColumn="Value"/>
            <Column Description="Min" MaxRange="1" MinRange="0" Name="Min" SQLDataType="2" SourceColumn="Min"/>
            <Column Description="Max" MaxRange="1" MinRange="0" Name="Max" SQLDataType="2" SourceColumn="Max"/>
            <Column Description="ObjectDisplay" MaxRange="1" MinRange="0" Name="ObjectDisplay" SQLDataType="12" SourceColumn="ObjectDisplay"/>
            <Column Description="Datatype" MaxRange="1" MinRange="0" Name="Datatype" SQLDataType="-9" SourceColumn="Datatype"/>
        </Columns>
        <Row>
            <Name>Tank</Name>
            <Value>T111</Value>
            <Min>1</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Material</Name>
            <Value>111-Bulk</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Batch</Name>
            <Value>1111111</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
    </Rowset>
    <Rowset>
        <Columns>
            <Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="-9" SourceColumn="Name"/>
            <Column Description="Value" MaxRange="1" MinRange="0" Name="Value" SQLDataType="-9" SourceColumn="Value"/>
            <Column Description="Min" MaxRange="1" MinRange="0" Name="Min" SQLDataType="2" SourceColumn="Min"/>
            <Column Description="Max" MaxRange="1" MinRange="0" Name="Max" SQLDataType="2" SourceColumn="Max"/>
            <Column Description="ObjectDisplay" MaxRange="1" MinRange="0" Name="ObjectDisplay" SQLDataType="12" SourceColumn="ObjectDisplay"/>
            <Column Description="Datatype" MaxRange="1" MinRange="0" Name="Datatype" SQLDataType="-9" SourceColumn="Datatype"/>
        </Columns>
        <Row>
            <Name>Tank</Name>
            <Value>T222</Value>
            <Min>1</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Material</Name>
            <Value>222-Bulk</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Batch</Name>
            <Value>2222222</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
    </Rowset>
    <Rowset>
        <Columns>
            <Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="-9" SourceColumn="Name"/>
            <Column Description="Value" MaxRange="1" MinRange="0" Name="Value" SQLDataType="-9" SourceColumn="Value"/>
            <Column Description="Min" MaxRange="1" MinRange="0" Name="Min" SQLDataType="2" SourceColumn="Min"/>
            <Column Description="Max" MaxRange="1" MinRange="0" Name="Max" SQLDataType="2" SourceColumn="Max"/>
            <Column Description="ObjectDisplay" MaxRange="1" MinRange="0" Name="ObjectDisplay" SQLDataType="12" SourceColumn="ObjectDisplay"/>
            <Column Description="Datatype" MaxRange="1" MinRange="0" Name="Datatype" SQLDataType="-9" SourceColumn="Datatype"/>
        </Columns>
        <Row>
            <Name>Tank</Name>
            <Value>T333</Value>
            <Min>1</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Material</Name>
            <Value>333-Bulk</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
        <Row>
            <Name>Batch</Name>
            <Value>3333333</Value>
            <Min>7</Min>
            <Max>20</Max>
            <ObjectDisplay>Enabled</ObjectDisplay>
            <Datatype>String</Datatype>
        </Row>
    </Rowset>
</Rowsets>

XSLT 1.0

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

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <Rowset>
                <Columns>
                    <xsl:apply-templates select="Rowset[1]/Row" mode="cols"/>
                </Columns>
                <xsl:apply-templates select="Rowset"/>
            </Rowset>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Row" mode="cols">
        <Column Description="" MaxRange="1" MinRange="0" 
            Name="{Name}" SQLDataType="1" SourceColumn="{Name}"/>
    </xsl:template>

    <xsl:template match="Rowset">
        <Row>
            <xsl:apply-templates select="Row/Name"/>            
        </Row>
    </xsl:template>

    <xsl:template match="Name">
        <xsl:element name="{.}">
            <xsl:value-of select="../Value"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

XML 输出

<Rowsets CachedTime=""
         DateCreated="2013-07-30T00:13:32"
         EndDate="2013-07-30T00:13:29"
         StartDate="2013-07-29T23:13:29"
         Version="12.2.4 Build(92)">
   <Rowset>
      <Columns>
         <Column Description=""
                 MaxRange="1"
                 MinRange="0"
                 Name="Tank"
                 SQLDataType="1"
                 SourceColumn="Tank"/>
         <Column Description=""
                 MaxRange="1"
                 MinRange="0"
                 Name="Material"
                 SQLDataType="1"
                 SourceColumn="Material"/>
         <Column Description=""
                 MaxRange="1"
                 MinRange="0"
                 Name="Batch"
                 SQLDataType="1"
                 SourceColumn="Batch"/>
      </Columns>
      <Row>
         <Tank>T111</Tank>
         <Material>111-Bulk</Material>
         <Batch>1111111</Batch>
      </Row>
      <Row>
         <Tank>T222</Tank>
         <Material>222-Bulk</Material>
         <Batch>2222222</Batch>
      </Row>
      <Row>
         <Tank>T333</Tank>
         <Material>333-Bulk</Material>
         <Batch>3333333</Batch>
      </Row>
   </Rowset>
</Rowsets>
于 2013-09-11T07:52:04.703 回答