我是xslt新手。我有以下问题。我需要在xml中从特定元素(例如div)中删除特定属性(在本例中为theAttribute)。即
<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>成为
<html>
<head>...</head>
<body>
<div id="qaz">
</div>
<div id ="ddd">
<div id= "ggg">
</div>
</div>
<font theAttribute="foo" />
</body>
</html>其中属性theAtribute已被删除。我找到了这个,http://www.biglist.com/lists/xsl-list/archives/200404/msg00668.html,基于它我试图找到合适的解决方案。
即<xsl:template match="@theAtribute" />
将其从整个文档中删除...还有其他的,比如match,if choose等等,都不起作用..:-(你能在这方面帮我一下吗?对我来说,这听起来微不足道,但是使用xslt,我根本无法应付……
提前感谢所有人
发布于 2010-06-25 19:18:05
什么不起作用?您是否想要相同的内容,只是没有@theAtribute
如果是这样,请确保您的样式表具有@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>如果您只想抑制某些@theAtribute,则可以使匹配条件更加具体。例如,如果您只想从div who's @id="qaz"中删除该属性,则可以使用以下模板:
<xsl:template match="@theAtribute[../@id='qaz']" />或此模板:
<xsl:template match="*[@id='qaz']/@theAtribute" />如果要从所有div元素中删除@theAttribute,请将匹配表达式更改为:
<xsl:template match="div/@theAtribute" />发布于 2011-05-11 16:19:18
在select中,可以使用name函数排除(或包含)该属性。
例如,<xsl:copy-of select="@*[name(.)!='theAtribute']|node()" />
发布于 2019-08-01 21:05:38
<?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>我有一个像这样的xml:
<row attribute1="value" >some stuff</row> 我在属性列表的末尾添加了一个来自外部值的状态。
\saxonee\bin\Transform.exe -xsl:my_script.xsl -s:rows.xml status=“已完成”
<row current_run="2019-07-29 19:00" status="completed">https://stackoverflow.com/questions/3116396
复制相似问题