替换XSLT中的多个属性(使用标识模板)

Rah*_*ulD 1 xml xslt xhtml xslt-1.0

您好我必须使用XSLT替换给定标记的多个属性,同时使用标识模板复制整个文件.使用我给定的XSLT,我可以替换一个属性(类的值)而不是另一个属性. 输入文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 11 February 2007), see www.w3.org" />
  <meta http-equiv="Content-type" content="text/html; charset=us-ascii" />

  <title></title>
</head>

<body>
  <!--OTHER STUFF-->    
  <div class="LR" id="12">
  </div>
  <!--OTHER STUFF-->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

输出文件:

<?xml version="1.0" encoding="utf-8"?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 11 February 2007), see www.w3.org" />
  <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />

  <title></title>
</head>

<body>
  <!--OTHER STUFF-->
  <div class="WZ" id="56">
  </div>
  <!--OTHER STUFF-->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的XSLT:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xhtml="http://www.w3.org/1999/xhtml"
 xmlns="http://www.w3.org/1999/xhtml"
 exclude-result-prefixes="xhtml">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*" />

<xsl:template match="@*|node()">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="xhtml:div[@id='12']/@class">
  <xsl:attribute name="class">WZ</xsl:attribute>
   <xsl:attribute name="id">56</xsl:attribute> 
</xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

感谢您!

Sea*_*kin 6

此模板还复制<div>节点可能具有的任何额外属性,因此它是一个更强大的解决方案.

这个模板......

<xsl:template match="xhtml:div[@id='12']"
  xmlns="http://www.w3.org/1999/xhtml">
  <div id="56" class="WZ">
    <xsl:apply-templates select="
      (@*[local-name()!='id']
         [local-name()!='class'])
      | node()"/>
  </div>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

...将改变这个元素......

<div class="LR" id="12" other="x">
Run Code Online (Sandbox Code Playgroud)

... INTO ...

<div id="56" class="WZ" other="x" />
Run Code Online (Sandbox Code Playgroud)

请注意,保留额外属性"other"!


更新

除了回答问题之外,更多的是一般兴趣,这里有几个等价的替代品......

XSLT 1.0替代方案

这是另一个XSLT 1.0模板,它做同样的事情.效率较低但更简洁(好吧,有点)......

<xsl:template match="xhtml:div[@id='12']"
  xmlns="http://www.w3.org/1999/xhtml">
  <div>
    <xsl:apply-templates select="@*" />
    <xsl:attribute name="id">56</xsl:attribute>
    <xsl:attribute name="class">WZ</xsl:attribute>
    <xsl:apply-templates select="node()" />
  </div>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

XSLT 2.0替代方案

我知道OP正在使用XSLT 1.0.这只是为了兴趣.想要简洁和高效吗?从不畏惧!这个XSLT 2.0替代方案的帮助就在这里......

<xsl:template match="xhtml:div[@id='12']"
  xmlns="http://www.w3.org/1999/xhtml">
  <div id="56" class="WZ">
    <xsl:apply-templates select="@* except (@id,@class) | node()" />
  </div>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)