【问题标题】:Replacing more than one attributes in XSLT (with identity template)替换 XSLT 中的多个属性(使用标识模板)
【发布时间】:2012-07-23 11:10:06
【问题描述】:

您好,在使用标识模板复制整个文件时,我必须使用 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>

谢谢!

【问题讨论】:

    标签: xml xslt xhtml xslt-1.0


    【解决方案1】:

    此模板还可以复制

    节点可能具有的任何额外属性,因此它是一个更强大的解决方案。

    此模板...

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

    ...进入...

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

    注意额外的属性“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>
    

    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>
    

    【讨论】:

    • 再次感谢您的详细解答。 :)
    【解决方案2】:

    尝试匹配@*:

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

    【讨论】:

    • 非常感谢您的完美回答。
    • 更通用的解决方案会更好。这样做的问题是,如果 div[@id=12] 节点具有除 class 和 div 之外的其他属性,这些属性将丢失,这可能不是 OP 想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2019-05-15
    • 2013-06-29
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多