2014-05-12 179 views
1

我試圖刪除節點名稱,但保留內容或將節點的內容複製到其父節點。我從SSRS導出XML,並將XSLT文件附加到報表RDL。我覺得我很喜歡我的xslt。我試圖刪除文件中頻繁重複的節點「Cell」。XSLT刪除節點但保留內容

我想這一點:

<ReportSectionAPercentLabel> 
    <Cell> 
     <losPct>0.262158054711246</losPct> 
    </Cell> 
</ReportSectionAPercentLabel> 

看起來像這樣:

<ReportSectionAPercentLabel> 
    <losPct>0.262158054711246</losPct> 
</ReportSectionAPercentLabel> 

這是XML的excert:

<?xml version="1.0" encoding="UTF8"?> 
<Report xmlns="My_Report" Name="My report"> 
<ReportSectionATablix> 
    <ReportSectionARowGroup_Collection> 
     <ReportSectionARowGroup> 
      <losProvider>Your Enterprise</losProvider> 
      <ReportSectionAColumnGroup_Collection> 
       <ReportSectionAColumnGroup> 
        <ReportSectionAGroupLabel>07</ReportSectionAGroupLabel> 
        <ReportSectionACountLabel> 
         <Cell> 
          <ReportSectionACount>345</ReportSectionACount> 
         </Cell> 
        </ReportSectionACountLabel> 
        <ReportSectionAPercentLabel> 
         <Cell> 
          <losPct>0.262158054711246</losPct> 
         </Cell> 
        </ReportSectionAPercentLabel> 
       </ReportSectionAColumnGroup> 
       <ReportSectionAColumnGroup> 
        <ReportSectionAGroupLabel>814</ReportSectionAGroupLabel> 
        <ReportSectionACountLabel> 
         <Cell> 
          <ReportSectionACount>153</ReportSectionACount> 
         </Cell> 
       </ReportSectionACountLabel> 
       ... 

這是XSLT。我有Xpath到單元格錯誤嗎?

<xsl:template match="node()|@*" > 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*" /> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Cell" > 
    <xsl:apply-templates select="*" /> 
</xsl:template> 

回答

3

你的做法是正確的,但問題是命名空間之一。在你的XML,你有一個默認命名空間聲明(在的xmlns

<Report xmlns="My_Report" Name="My report"> 

這意味着元素,和所有的子孫,屬於該命名空間(除非被其他命名空間聲明覆蓋)。然而,在你的XSLT,在那裏你已指定細胞爲模板匹配,這是尋找NO命名空間的細胞元素,不會與命名空間中的XML你細胞匹配。

的解決方案是聲明在XSLT命名空間也用一個前綴,並使用該前綴在細胞元件匹配:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:rep="My_Report"> 
    <xsl:template match="node()|@*" > 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="rep:Cell" > 
     <xsl:apply-templates select="*" /> 
    </xsl:template> 
</xsl:stylesheet> 

,或者,如果在使用XSLT 2.0,可以使用XPath的默認名稱空間,在這種情況下沒有命名空間前綴任何XPath表達式,被認爲是在默認的命名空間

這也將工作...

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xpath-default-namespace="My_Report"> 
    <xsl:template match="node()|@*" > 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Cell" > 
     <xsl:apply-templates select="*" /> 
    </xsl:template> 
</xsl:stylesheet> 

如果出於某種原因,你想避免在XSLT編碼的命名空間聲明,你可以在模板匹配更改爲以下:

<xsl:template match="*[local-name()='Cell']" > 
+0

謝謝你的sooo多的解釋。 A)我不瞭解名稱空間,B)我現在知道,不是使用標準模板作爲SSRS輸出,而是需要爲每個名稱空間指定名稱空間。謝謝! – BClaydon