1

I have XML file. I want to copy it and remove these attributes: ExpandedColumnCount, ExpandedRowCount. How can I do it?

<Table ss:ExpandedColumnCount="7" ss:ExpandedRowCount="3" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
   <Row ss:AutoFitHeight="0"/>
   <Row ss:Index="3" ss:AutoFitHeight="0">
    <Cell ss:Index="4"><Data ss:Type="String">c1</Data></Cell>
    <Cell><Data ss:Type="String">c2</Data></Cell>
    <Cell><Data ss:Type="String">c3</Data></Cell>
    <Cell><Data ss:Type="String">c4</Data></Cell>
   </Row>
  </Table>

If I use this XSLT file I remove element Table.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="Table|@ExpandedColumnCount"/>
</xsl:stylesheet>

What I want:

<Table x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15">
   <Row ss:AutoFitHeight="0"/>
   <Row ss:Index="3" ss:AutoFitHeight="0">
    <Cell ss:Index="4"><Data ss:Type="String">c1</Data></Cell>
    <Cell><Data ss:Type="String">c2</Data></Cell>
    <Cell><Data ss:Type="String">c3</Data></Cell>
    <Cell><Data ss:Type="String">c4</Data></Cell>
   </Row>
  </Table>

What am I doing wrong?

kjhughes
  • 106,133
  • 27
  • 181
  • 240

1 Answers1

0

Replace

<xsl:template match="Table|@ExpandedColumnCount"/>

with

<xsl:template match="@ss:ExpandedColumnCount|@ss:ExpandedRowCount"/>

And define the namespace prefixes in your XML,

<Table xmlns:ss="http://example.com/ss"
       xmlns:x="http://example.com/x"
       ss:ExpandedColumnCount="7" ss:ExpandedRowCount="3" x:FullColumns="1"
       x:FullRows="1" ss:DefaultRowHeight="15">
  <Row ss:AutoFitHeight="0"/>
  <Row ss:Index="3" ss:AutoFitHeight="0">
    <Cell ss:Index="4"><Data ss:Type="String">c1</Data></Cell>
    <Cell><Data ss:Type="String">c2</Data></Cell>
    <Cell><Data ss:Type="String">c3</Data></Cell>
    <Cell><Data ss:Type="String">c4</Data></Cell>
  </Row>
</Table>

and in your XSLT,

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ss="http://example.com/ss">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="@ss:ExpandedColumnCount|@ss:ExpandedRowCount"/>
</xsl:stylesheet>

then you'll get the results you want:

<?xml version="1.0" encoding="UTF-8"?>
<Table xmlns:ss="http://example.com/ss"
       xmlns:x="http://example.com/x"
       x:FullColumns="1"
       x:FullRows="1"
       ss:DefaultRowHeight="15">
   <Row ss:AutoFitHeight="0"/>
   <Row ss:Index="3" ss:AutoFitHeight="0">
      <Cell ss:Index="4">
         <Data ss:Type="String">c1</Data>
      </Cell>
      <Cell>
         <Data ss:Type="String">c2</Data>
      </Cell>
      <Cell>
         <Data ss:Type="String">c3</Data>
      </Cell>
      <Cell>
         <Data ss:Type="String">c4</Data>
      </Cell>
   </Row>
</Table>

Update per question in comments: XSLT that defeats namespaces (not recommended) to remove the targeted attributes:

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ss="http://example.com/ss">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="@*">
    <xsl:if test="local-name()!='ExpandedColumnCount' and
                  local-name()!='ExpandedRowCount'">
      <xsl:copy/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • thanks! It work. Can we do without namespace prefixes? – Gevorg Arutiunian Jun 27 '17 at 12:05
  • Well, your XML is not [namespace-well-formed](https://stackoverflow.com/a/25830482/290085) without defining the namespace prefixes. It is possible to [defeat namespaces](https://stackoverflow.com/a/40796315/290085), but you really shouldn't. – kjhughes Jun 27 '17 at 12:17