在XSLT更换不止一个属性(与身份模板)(Replacing more than one attri

2019-10-17 02:24发布

您好我不得不更换使用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>

输出文件:

<?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>

我的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>

感谢您!

Answer 1:

尝试匹配@*

<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']/@*">
    <xsl:attribute name="class">WZ</xsl:attribute>
    <xsl:attribute name="id">56</xsl:attribute>
</xsl:template>
</xsl:stylesheet>

这使期望的结果:

<div class="WZ" id="56"></div>


Answer 2:

该模板还通过任何额外的副本属性,在<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>

... ...将变换分析这个元素?

<div class="LR" id="12" other="x">

... INTO ...

<div id="56" class="WZ" other="x" />

请注意,附加属性“其他”被保留!


更新

更多比回答的问题普遍关注,这里是一对夫妇等同替换的...

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>

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>


文章来源: Replacing more than one attributes in XSLT (with identity template)