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?
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.