我随身携带多个Rowset的XML。我想将所有Rowset合并为一个。那么请问我如何使用XSLT或任何其他方法来做到这一点?
当前XML:
<?xml version="1.0" encoding="utf-8"?>
<Rowsets DateCreated="2012-07-17T11:57:07" EndDate="2012-07-17T11:57:07" StartDate="2012-07-17T10:57:07" Version="12.0.12 Build(9)">
<Rowset>
<Columns>
<Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="12" SourceColumn="Name"/>
<Column Description="City" MaxRange="1" MinRange="0" Name="City" SQLDataType="4" SourceColumn="City"/>
<Column Description="Phone" MaxRange="1" MinRange="0" Name="Phone" SQLDataType="12" SourceColumn="Phone"/>
</Columns>
<Row>
<Name>Philip</Name>
<City>London</City>
<Phone>123</Phone>
</Row>
<Row>
<Name>Derek</Name>
<City>Seattle</City>
<Phone>500</Phone>
</Row>
<Row>
<Name>Bruke</Name>
<City>LosAngeles</City>
<Phone>600</Phone>
</Row>
<Rowset>
<Columns>
<Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="12" SourceColumn="Name"/>
<Column Description="City" MaxRange="1" MinRange="0" Name="City" SQLDataType="4" SourceColumn="City"/>
<Column Description="Phone" MaxRange="1" MinRange="0" Name="Phone" SQLDataType="12" SourceColumn="Phone"/>
</Columns>
<Row>
<Name>Yang</Name>
<City>SFO</City>
<Phone>1233</Phone>
</Row>
<Row>
<Name>Cristina</Name>
<City>SanJose</City>
<Phone>890</Phone>
</Row>
</Rowset>
<Rowset>
<Columns>
<Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="12" SourceColumn="Name"/>
<Column Description="City" MaxRange="1" MinRange="0" Name="City" SQLDataType="4" SourceColumn="City"/>
<Column Description="Phone" MaxRange="1" MinRange="0" Name="Phone" SQLDataType="12" SourceColumn="Phone"/>
</Columns>
<Row>
<Name>Meredith</Name>
<City>Sunnyvale</City>
<Phone>788</Phone>
</Row>
<Row>
<Name>Grey</Name>
<City>MountainView</City>
<Phone>456</Phone>
</Row>
<Row>
<Name>Torrence</Name>
<City>SAntaClara</City>
<Phone>432</Phone>
</Row>
</Rowset>
</Rowset>
</Rowsets>
输出所需的XML:
<?xml version="1.0" encoding="utf-8"?>
<Rowsets DateCreated="2012-07-17T11:57:07" EndDate="2012-07-17T11:57:07" StartDate="2012-07-17T10:57:07" Version="12.0.12 Build(9)">
<Rowset>
<Row>
<Name>Philip</Name>
<City>London</City>
<Phone>123</Phone>
</Row>
<Row>
<Name>Derek</Name>
<City>Seattle</City>
<Phone>500</Phone>
</Row>
<Row>
<Name>Bruke</Name>
<City>LosAngeles</City>
<Phone>600</Phone>
</Row>
<Row>
<Name>Yang</Name>
<City>SFO</City>
<Phone>1233</Phone>
</Row>
<Row>
<Name>Cristina</Name>
<City>SanJose</City>
<Phone>890</Phone>
</Row>
<Row>
<Name>Meredith</Name>
<City>Sunnyvale</City>
<Phone>788</Phone>
</Row>
<Row>
<Name>Grey</Name>
<City>MountainView</City>
<Phone>456</Phone>
</Row>
<Row>
<Name>Torrence</Name>
<City>SAntaClara</City>
<Phone>432</Phone>
</Row>
</Rowset>
</Rowsets>
那么你们这些人能帮助我怎样才能实现这个目标?
谢谢!
答案 0 :(得分:0)
您可以通过使用标准XSLT标识转换来实现此目的,但不是匹配和复制所有节点,而是只复制行元素
<xsl:template match="@*|Row|Row/*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
对于其他元素,您可以匹配它们,但继续处理它们的子节点而不复制它们
<xsl:template match="*">
<xsl:apply-templates select="node()"/>
</xsl:template>
您还需要一个模板来匹配根元素。
这是完整的XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Rowsets">
<Rowsets>
<xsl:apply-templates select="@*"/>
<Rowset>
<xsl:apply-templates select="node()"/>
</Rowset>
</Rowsets>
</xsl:template>
<xsl:template match="@*|Row|Row/*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:apply-templates select="node()"/>
</xsl:template>
</xsl:stylesheet>
应用于输入样本时,输出以下内容
<Rowsets DateCreated="2012-07-17T11:57:07" EndDate="2012-07-17T11:57:07" StartDate="2012-07-17T10:57:07" Version="12.0.12 Build(9)">
<Rowset>
<Row>
<Name>Philip</Name>
<City>London</City>
<Phone>123</Phone>
</Row>
<Row>
<Name>Derek</Name>
<City>Seattle</City>
<Phone>500</Phone>
</Row>
<Row>
<Name>Bruke</Name>
<City>LosAngeles</City>
<Phone>600</Phone>
</Row>
<Row>
<Name>Yang</Name>
<City>SFO</City>
<Phone>1233</Phone>
</Row>
<Row>
<Name>Cristina</Name>
<City>SanJose</City>
<Phone>890</Phone>
</Row>
<Row>
<Name>Meredith</Name>
<City>Sunnyvale</City>
<Phone>788</Phone>
</Row>
<Row>
<Name>Grey</Name>
<City>MountainView</City>
<Phone>456</Phone>
</Row>
<Row>
<Name>Torrence</Name>
<City>SAntaClara</City>
<Phone>432</Phone>
</Row>
</Rowset>
</Rowsets>
答案 1 :(得分:0)
我能够根据Tim的答案编写简化的XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Rowsets DateCreated="{Rowsets/@DateCreated}" Version="{Rowsets/@Version}" StartDate="{Rowsets/@StartDate}" EndDate="{Rowsets/@EndDate}">
<Rowset>
<xsl:for-each select="Rowsets/Rowset">
<xsl:variable name="RowsetNo">
<xsl:value-of select="position()"/>
</xsl:variable>
<xsl:if test="$RowsetNo = 1">
<xsl:copy-of select="Columns"/>
</xsl:if>
<xsl:copy-of select="Row"/>
</xsl:for-each>
</Rowset>
</Rowsets>
</xsl:template>
我希望它有所帮助。
Soham