使用 xslt 从特定的 xml 元素中排除属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3116396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exclude attribute from a specific xml element using xslt
提问by Alexandros
I am new in xslt. I have the following problem. I need within an xml, to remove a specific attribute (theAttributein the example) from a specific element (e.g. div).
i.e.
我是 xslt 的新手。我有以下问题。我需要在 xml 中theAttribute从特定元素(例如div)中删除特定属性(在示例中)。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. I found this, http://www.biglist.com/lists/xsl-list/archives/200404/msg00668.htmlbased on which i made attempts to find the proper solution.
其中属性 theAtribute 已被删除。我发现了这一点, http://www.biglist.com/lists/xsl-list/archives/200404/msg00668.html基于它我试图找到合适的解决方案。
i.e. <xsl:template match="@theAtribute" />
IE <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
谢谢大家
回答by Mads Hansen
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. For instance, if you only wanted to remove that attribute from the divwho's @id="qaz", then you could use this template:
如果您只想抑制某些@theAtribute,那么您可以使匹配条件更加具体。例如,如果您只想从divwho's 中删除该属性@id="qaz",那么您可以使用此模板:
<xsl:template match="@theAtribute[../@id='qaz']" />
or this template:
或这个模板:
<xsl:template match="*[@id='qaz']/@theAtribute" />
If you want to remove @theAttributefrom all divelements, then change the match expression to:
如果@theAttribute要从所有div元素中删除,则将匹配表达式更改为:
<xsl:template match="div/@theAtribute" />
回答by lwpro2
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()" />
回答by user1329485
<?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:
我有一个像:
<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="已完成"
<row current_run="2019-07-29 19:00" status="completed">

