替换 XSLT 中的多个属性(使用身份模板) [英] Replacing more than one attributes in XSLT (with identity template)

查看:25
本文介绍了替换 XSLT 中的多个属性(使用身份模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<html xmlns="http://www.w3.org/1999/xhtml"><头><元名称=生成器"内容=Linux/x86 的 HTML Tidy(2007 年 2 月 11 日),请参阅 www.w3.org"/><meta http-equiv="Content-type" content="text/html; charset=us-ascii"/><title></title><身体><!--其他东西-->

<!--其他东西--></html>

输出文件:

<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"/><title></title><身体><!--其他东西-->

<!--其他东西--></html>

我的 XSLT:

<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:模板><xsl:template match="xhtml:div[@id='12']/@class"><xsl:attribute name="class">WZ</xsl:attribute><xsl:attribute name="id">56</xsl:attribute></xsl:模板></xsl:stylesheet>

谢谢!

解决方案

尝试匹配 @*:

<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:模板><xsl:template match="xhtml:div[@id='12']/@*"><xsl:attribute name="class">WZ</xsl:attribute><xsl:attribute name="id">56</xsl:attribute></xsl:模板></xsl:stylesheet>

这给出了想要的结果:

Hi I have to replace more than one attributes of a given tag using XSLT while copying the entire file with identity template. With my given XSLT I'm able to replace one attribute (value of class) but not the other one. Input file:

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

Output file:

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

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

Thanking you!

解决方案

Try matching @*:

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

This gives the desired result:

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

这篇关于替换 XSLT 中的多个属性(使用身份模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆