[英]How to remove unnecessary attributes from element in XML using XSLT?
I have XML file. 我有XML文件。 I want to copy it and remove these attributes:
ExpandedColumnCount
, ExpandedRowCount
. 我想复制它并删除这些属性:
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
. 如果我使用这个XSLT文件,我删除元素
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, 并在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, 在你的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: 注释中的每个问题更新: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="@*">
<xsl:if test="local-name()!='ExpandedColumnCount' and
local-name()!='ExpandedRowCount'">
<xsl:copy/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.