[英]Exclude attribute from a specific xml element using xslt
I am new in xslt. 我是xslt的新手。 I have the following problem. 我有以下问题。 I need within an xml, to remove a specific attribute ( theAttribute
in the example) from a specific element (eg div
). 我需要在xml中删除特定元素(例如div
)中的特定属性( theAttribute
中的theAttribute
)。 ie 即
<html>
<head>...</head>
<body>
<div id="qaz" theAtribute="44">
</div>
<div id ="ddd" theAtribute="4">
<div id= "ggg" theAtribute="9">
</div>
</div>
<font theAttribute="foo" />
</body>
</html>
to become 成为
<html>
<head>...</head>
<body>
<div id="qaz">
</div>
<div id ="ddd">
<div id= "ggg">
</div>
</div>
<font theAttribute="foo" />
</body>
</html>
Where attribute theAtribute has been removed. where属性theAtribute已被删除。 I found this, http://www.biglist.com/lists/xsl-list/archives/200404/msg00668.html based on which i made attempts to find the proper solution. 我找到了这个, http://www.biglist.com/lists/xsl-list/archives/200404/msg00668.html ,我试图找到合适的解决方案。
ie <xsl:template match="@theAtribute" />
即<xsl:template match="@theAtribute" />
Which removed it from the whole document... and others like match, if choose, etc. Nothing worked.. :-( can you please help me on this? it sound trivial to me, but with xslt, i cannot cope at all... 从整个文件中删除它...和其他像匹配,如果选择等等没有什么工作.. :-(你能帮我这个吗?这对我来说听起来微不足道,但是对于xslt,我根本无法应付...
Thank you all in advance 谢谢大家
What is not working? 什么不起作用? Do you want the same content, just without the @theAtribute
? 你想要相同的内容,只是没有@theAtribute
?
If so, make sure your stylesheet has the empty template for @theAtribute
, but also has an identity template that copies everything else into the output: 如果是这样,请确保样式表具有@theAtribute
的空模板,但也有一个标识模板,可将其他所有内容复制到输出中:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--empty template suppresses this attribute-->
<xsl:template match="@theAtribute" />
<!--identity template copies everything forward by default-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
If you only want to suppress certain @theAtribute
, then you can make the match criteria more specific. 如果您只想禁止某些@theAtribute
,则可以使匹配条件更具体。 For instance, if you only wanted to remove that attribute from the div
who's @id="qaz"
, then you could use this template: 例如,如果您只想从div
的@id="qaz"
删除该属性,那么您可以使用此模板:
<xsl:template match="@theAtribute[../@id='qaz']" />
or this template: 或者这个模板:
<xsl:template match="*[@id='qaz']/@theAtribute" />
If you want to remove @theAttribute
from all div
elements, then change the match expression to: 如果要从所有div
元素中删除@theAttribute
,请将匹配表达式更改为:
<xsl:template match="div/@theAtribute" />
inside the select, you can exclude (or include,) the attribute, using the name function. 在select中,您可以使用name函数排除(或包含)该属性。
For example, <xsl:copy-of select="@*[name(.)!='theAtribute']|node()" />
例如, <xsl:copy-of select="@*[name(.)!='theAtribute']|node()" />
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:param name="status"/>
<!--If attribute is present change it -->
<xsl:template match="@status" >
<xsl:attribute name="status" select="$status"/>
</xsl:template>
<!-- If attribute is not present add it al last position -->
<xsl:template match="row[ not( @status ) ]" >
<xsl:copy>
<xsl:apply-templates select="@*"/>
<!-- put attribute in the last position -->
<xsl:attribute name="status" select="$status"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<!--identity template copies everything forward by default-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I have a xml like: 我有一个像xml:
<row attribute1="value" >some stuff</row>
and I'm adding at end of list of attributes a status from an outside value. 我在属性列表的末尾添加了一个来自外部值的状态。
\\saxonee\\bin\\Transform.exe -xsl:my_script.xsl -s:rows.xml status="completed" \\ saxonee \\ bin \\ Transform.exe -xsl:my_script.xsl -s:rows.xml status =“completed”
<row current_run="2019-07-29 19:00" status="completed">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.