首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用xslt从特定xml元素中排除属性

使用xslt从特定xml元素中排除属性
EN

Stack Overflow用户
提问于 2010-06-25 15:54:34
回答 3查看 29.2K关注 0票数 18

我是xslt新手。我有以下问题。我需要在xml中从特定元素(例如div)中删除特定属性(在本例中为theAttribute)。即

代码语言:javascript
复制
<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>

成为

代码语言:javascript
复制
<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,我根本无法应付……

提前感谢所有人

EN

Stack Overflow用户

发布于 2019-08-01 21:05:38

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<row  attribute1="value" >some stuff</row> 

我在属性列表的末尾添加了一个来自外部值的状态。

\saxonee\bin\Transform.exe -xsl:my_script.xsl -s:rows.xml status=“已完成”

代码语言:javascript
复制
<row current_run="2019-07-29 19:00" status="completed">
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3116396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档