XSLT输出提供额外的<columns>节点

时间:2015-12-16 22:26:16

标签: xml xslt

我的最终XML应该采用以下格式:

router.post('/some-irl', function (req, res) {
    //some code
    return {some JSON}
});

我目前正在合并两个XML,它为我提供了以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<Rowsets>
    <Rowset>
        <Columns></Columns>
        <Row></Row>
        <Row></Row>
        <Row></Row>     
    </Rowset>
</Rowsets>

现在我只需要一个<?xml version="1.0" encoding="UTF-8"?><Rowsets> <Rowset> <Columns> <Column Description="" MaxRange="1" MinRange="0" Name="Material" SQLDataType="1" SourceColumn="Material"/> <Column Description="" MaxRange="1" MinRange="0" Name="DS_MATERIAL" SQLDataType="1" SourceColumn="DS_MATERIAL"/> <Column Description="" MaxRange="1" MinRange="0" Name="Press" SQLDataType="1" SourceColumn="Press"/> <Column Description="" MaxRange="1" MinRange="0" Name="Mold" SQLDataType="1" SourceColumn="Mold"/> </Columns> <Row> <Material>300-6953</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>14</Press> <Mold>3864F</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>25</Press> <Mold>4306K-1</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>25</Press> <Mold>4306K-1</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>Summary</Press> <Mold>---</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>23</Press> <Mold>4305P-1</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>23</Press> <Mold>4305P-1</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>---</DS_MATERIAL> <Press>Summary</Press> <Mold>---</Mold> </Row> </Rowset> <Rowset> <Columns> <Column Description="" MaxRange="1" MinRange="0" Name="Material" SQLDataType="1" SourceColumn="Material"/> <Column Description="" MaxRange="1" MinRange="0" Name="DS_MATERIAL" SQLDataType="1" SourceColumn="DS_MATERIAL"/> <Column Description="" MaxRange="1" MinRange="0" Name="Press" SQLDataType="1" SourceColumn="Press"/> <Column Description="" MaxRange="1" MinRange="0" Name="Mold" SQLDataType="1" SourceColumn="Mold"/> </Columns> <Row> <Material>300-6953</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>14</Press> <Mold>3864F</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>25</Press> <Mold>4306K-1</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>25</Press> <Mold>4306K-1</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>Summary</Press> <Mold>---</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>23</Press> <Mold>4305P-1</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>23</Press> <Mold>4305P-1</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>Summary</Press> <Mold>---</Mold> </Row> </Rowset> </Rowsets> 以及<Rowset>内的所有<Row>,我正在将以下XSLT应用于上面的XML:

<Rowset>

这将在<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java" version="1.0"> <xsl:output media-type="text/xml" method="xml"/> <!-- Merges multiple rowsets --> <xsl:param name="SD"/> <xsl:param name="ED"/> <xsl:param name="RowCount"/> <xsl:template match="/"> <Rowsets> <Rowset> <xsl:copy-of select="/Rowsets/Rowset/Columns"/> <xsl:for-each select="Rowsets/Rowset"> <xsl:copy-of select="Row"/> </xsl:for-each> </Rowset> </Rowsets> </xsl:template> </xsl:stylesheet> 内创建所有<Row>,但会创建两个<Rowset>个节点。我只需要一个<Columns>节点。

以上XSLT的结果XML如下:

<Columns>

我的XSLT需要进行哪些修改才能删除这个额外的<?xml version="1.0" encoding="UTF-8"?><Rowsets> <Rowset> <Columns> <Column Description="" MaxRange="1" MinRange="0" Name="Material" SQLDataType="1" SourceColumn="Material"/> <Column Description="" MaxRange="1" MinRange="0" Name="DS_MATERIAL" SQLDataType="1" SourceColumn="DS_MATERIAL"/> <Column Description="" MaxRange="1" MinRange="0" Name="Press" SQLDataType="1" SourceColumn="Press"/> <Column Description="" MaxRange="1" MinRange="0" Name="Mold" SQLDataType="1" SourceColumn="Mold"/> </Columns> <Columns> <Column Description="" MaxRange="1" MinRange="0" Name="Material" SQLDataType="1" SourceColumn="Material"/> <Column Description="" MaxRange="1" MinRange="0" Name="DS_MATERIAL" SQLDataType="1" SourceColumn="DS_MATERIAL"/> <Column Description="" MaxRange="1" MinRange="0" Name="Press" SQLDataType="1" SourceColumn="Press"/> <Column Description="" MaxRange="1" MinRange="0" Name="Mold" SQLDataType="1" SourceColumn="Mold"/> </Columns> <Row> <Material>300-6953</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>14</Press> <Mold>3864F</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>25</Press> <Mold>4306K-1</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>25</Press> <Mold>4306K-1</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>Summary</Press> <Mold>---</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>23</Press> <Mold>4305P-1</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>111</DS_MATERIAL> <Press>23</Press> <Mold>4305P-1</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>---</DS_MATERIAL> <Press>Summary</Press> <Mold>---</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>14</Press> <Mold>3864F</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>25</Press> <Mold>4306K-1</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>25</Press> <Mold>4306K-1</Mold> </Row> <Row> <Material>300-6953</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>Summary</Press> <Mold>---</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>23</Press> <Mold>4305P-1</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>23</Press> <Mold>4305P-1</Mold> </Row> <Row> <Material>300-6953-1</Material> <DS_MATERIAL>222</DS_MATERIAL> <Press>Summary</Press> <Mold>---</Mold> </Row> </Rowset> </Rowsets> 节点?

我需要的XML如下:

<Columns>

请帮忙。

1 个答案:

答案 0 :(得分:1)

以这种方式试试吗?

XSLT 1.0

<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="*"/>

<xsl:template match="/Rowsets">
    <Rowsets>
        <Rowset>
            <xsl:copy-of select="Rowset[1]/Columns"/>
            <xsl:copy-of select="Rowset/Row"/>
        </Rowset>
    </Rowsets>
</xsl:template>

</xsl:stylesheet>
  

我目前正在合并两个XML,它为我提供了以下XML:

您可以在合并XSLT中修复此问题,并节省自己需要进行额外转换。