Xml XSLT删除节点但保留内容

Xml XSLT删除节点但保留内容,xml,xslt,reporting-services,Xml,Xslt,Reporting Services,我试图删除节点名称,但保留内容,或将节点的内容复制到其父节点。我正在从SSRS导出XML,并在报告RDL中附加了一个XSLT文件。我觉得我已经非常熟悉xslt了。我正在尝试删除文件中频繁重复的节点“Cell” 我想要这个: <ReportSectionAPercentLabel> <Cell> <losPct>0.262158054711246</losPct> </Cell> </ReportSe

我试图删除节点名称,但保留内容,或将节点的内容复制到其父节点。我正在从SSRS导出XML,并在报告RDL中附加了一个XSLT文件。我觉得我已经非常熟悉xslt了。我正在尝试删除文件中频繁重复的节点“Cell”

我想要这个:

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

0.262158054711246
看起来像这样:

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

0.262158054711246
这是XML的一个例外:

<?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>
                ...

你的企业
07
345
0.262158054711246
814
153
...
这是XSLT。我的单元格Xpath是否有误



您的方法是正确的,但问题在于名称空间。在XML中,您有一个默认的名称空间声明(xmlns)


这意味着元素和所有子元素都属于该名称空间(除非被另一个名称空间声明覆盖)。但是,在XSLT中,您指定了单元格作为模板匹配,这是在没有命名空间的情况下查找单元格元素,该元素不会将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>

或者,如果您使用的是XSLT2.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']" >

非常感谢您的解释。A) 我不了解名称空间,并且B)我现在知道,我需要为每个SSRS输出指定名称空间,而不是使用标准模板。谢谢
<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>
<xsl:template match="*[local-name()='Cell']" >