我有以下XML文件(它实际上是SQL服务器报告服务RDL文件)。我想将<Value>
中的TablixCell
替换为<DataField>
中Fields
的位置。也就是说,“order date1”和“prod id1”应分别替换为“order_date”和“prod_id”。
最好可以使用XQuery在SQL Server 2008中完成。如果不是,Xslt很好。
<Fields>
<Field Name="order_date">
<DataField>order_date</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="prod_id">
<DataField>prod_id</DataField>
<rd:TypeName>System.Int32</rd:TypeName>
</Field>
....
</Fields>
......
<TablixRows>
<TablixRow>
<Height>0.25in</Height>
<TablixCells>
<TablixCell>
......
<Value>order date1</Value>
......
</TablixCell>
<TablixCell>
.....
<Value>prod id1</Value>
.....
答案 0 :(得分:1)
尝试以下方法:
<?xml version="1.0" encoding="UTF-8"?>
<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:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="TablixCell">
<xsl:copy>
<xsl:apply-templates>
<xsl:with-param name="pos" select="position()"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="Value">
<xsl:param name="pos"/>
<xsl:copy>
<xsl:value-of select="//Fields/Field[$pos]/DataField"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
使用输入XML
运行时<?xml version="1.0" encoding="UTF-8"?>
<Fields>
<Field Name="order_date">
<DataField>order_date</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="prod_id">
<DataField>prod_id</DataField>
<rd:TypeName>System.Int32</rd:TypeName>
</Field>
<Field Name="xxx_id">
<DataField>zzz_id</DataField>
<rd:TypeName>System.String16</rd:TypeName>
</Field>
<TablixRows>
<TablixRow>
<Height>0.25in</Height>
<TablixCells>
<TablixCell>
<Value>order date1</Value>
</TablixCell>
<TablixCell>
<Value>prod id1</Value>
</TablixCell>
<TablixCell>
<Value>xxx id1</Value>
</TablixCell>
</TablixCells>
</TablixRow>
</TablixRows>
</Fields>
结果是必需的
<?xml version="1.0" encoding="UTF-8"?>
<Fields>
<Field Name="order_date">
<DataField>order_date</DataField>
<rd:TypeName>System.DateTime</rd:TypeName>
</Field>
<Field Name="prod_id">
<DataField>prod_id</DataField>
<rd:TypeName>System.Int32</rd:TypeName>
</Field>
<Field Name="xxx_id">
<DataField>zzz_id</DataField>
<rd:TypeName>System.String16</rd:TypeName>
</Field>
<TablixRows>
<TablixRow>
<Height>0.25in</Height>
<TablixCells>
<TablixCell>
<Value>order_date</Value>
</TablixCell>
<TablixCell>
<Value>prod_id</Value>
</TablixCell>
<TablixCell>
<Value>zzz_id</Value>
</TablixCell>
</TablixCells>
</TablixRow>
</TablixRows>
</Fields>