XSLT模式匹配 - 删除祖先

时间:2014-09-16 16:10:16

标签: xml regex xslt reporting-services

我将SSRS中的条形图数据导出为XML,大部分时间它看起来很棒但在某些情况下它会获得/ SeriesX / CategoryX / nodes。当.net将这些内容读入数据表时,dat会被拆分为三个相关的表; Series0,Category0和Value。

我需要从中删除/ SeriesX / CategoryX /:

  <My_Location_Chart>
    <Series0>
      <Category0>
        <Value Y="0.1111" />
      </Category0>
    </Series0>
    <Series1>
      <Category0>
        <Value Y="0.2222" />
      </Category0>
    </Series1>
    <Series2>
      <Category0>
        <Value Y="0.3333" />
      </Category0>
    </Series2>
  </My_Location_Chart>

所以它看起来像这样:

  <My_Location_Chart>
      <Value Y="0.1111" />
      <Value Y="0.2222" />
      <Value Y="0.3333" />
  </My_Location_Chart>

还有其他图表使用SeriesX值,我不想搞砸那些,但没有一个是/ SeriesX / CategoryX /的模式,所以我只想匹配那个模式。

我已尝试以下方法删除“类别”节点。它很接近,但不完全符合我的要求:

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

    <xsl:template match="*[not(descendant-or-self::*[text()[normalize-space()] | @*])]"/>


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

以上创造了这个:

  <My_Location_Chart>
    <Series0>
      <Value Y="0.1111" />
    </Series0>
    <Series1>
      <Value Y="0.2222" />
    </Series1>
    <Series2>
      <Value Y="0.3333" />
    </Series2>
  </My_Location_Chart>

欢迎任何建议。我是XSLT的新手,对我很轻松。谢谢!

1 个答案:

答案 0 :(得分:0)

简单的事情怎么样?

<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="/My_Location_Chart">
    <xsl:copy>
        <xsl:copy-of select="*/Category0/Value"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

请注意,这不会改变&#34; 0.711590296495957&#34;进入&#34; 0.813084112149533&#34;如你的例子所示。

相关问题