2012-03-15 54 views
0

我有以下XML文件(它實際上是SQL服務器報告服務RDL文件)。我想按位置替換TablixCell中的<Value>Fields中的<DataField>。也就是說,「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

你能解釋一下你」運行此重新嘗試完成?我最初的想法是,你可以在rdl文件中找到並替換......但是你試圖使用XQuery的事實讓我懷疑是否有比在這個文件中替換字符串更大的目標。 – Rose 2012-03-15 23:20:55

+0

是的,如果一個XML文件中只有幾個字段,查找和替換工作正常。但是,50份報告中有數百欄。 – ca9163d9 2012-03-15 23:38:50

+1

提供的XML文件摘錄中沒有「prod_date」。摘錄不是一個格式良好的XML文件,它甚至不是一個格式正確的XML片段。請修改並更正。 – 2012-03-16 01:28:07

回答

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>