XML to XML with XSLT - 添加、删除、修改元素和属性 [英] XML to XML with XSLT- Add, Remove, Modify Elements and Attributes

查看:33
本文介绍了XML to XML with XSLT - 添加、删除、修改元素和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 XSLT 从一个 XML (XHTML) 文件更改为另一个文件.在新的 XML 文件中,我必须删除/添加/修改一些元素.为此,我创建了一个 identity.xsl 文件,该文件复制了整个源文件,然后我创建了一个包含 identity.xsl 的新 XSLT,然后在该新 XSLT 中我正在尝试进行修改.我可以通过传递一个什么都不做的模板匹配来消除一些不需要的属性,但我无法在现有标签中添加新属性,也无法在特定位置添加新元素(在特定位置带有结束标签).

我的原始文件:

<html xmlns="http://www.w3.org/1999/xhtml"><头><meta http-equiv="Content-type" content="text/html; charset=utf-8"/><身体><div id="o"><div id="nd"><p>1</p>

<div class="TF id="id12"><element1 name="abc" src="abc.jpg"></script><input type="radio" id="1" event="xyz"><div class="q"><br/><div id="ta3" class="block"><span style="a">ABC</span>

<br/>T<输入/>F<输入/><div id="sf"><div id="ta3">

</html>

所需文件:

<html xmlns="http://www.w3.org/1999/xhtml"><头><meta http-equiv="Content-type" content="text/html; charset=utf-8"/><!--必须添加这两个元素--><元素添加="xyz" id="23"><元素添加="xyz" id="24"><!--在身体标签中添加属性--><body onLoad="ada" bgcolor="pink"><div id="o"><div id="nd"><p>1</p>

<div class="TF id="id12"><!--必须提升 SRC 属性的值--><element1 name="abc" src="xyz.jpg"></script><!--使用属性添加此表单元素--><表格名称=表格"><input type="radio" id="1" event="xyz"><div class="q"><br/><div id="ta3" class="block"><span style="a">ABC</span>

<br/>T<!--添加表/TR/TD标签--><表格><tr><td><输入/></td></tr><tr></td>F<输入/></td></tr><div id="sf"><div id="ta3">

<!--添加输入标签--><input type="submit" value="Done"/>

<!--关闭表单标签--></表单>

</html>

XSLT:

<!-- 导入身份转换.--><xsl:import href="identity.xsl"/><xsl:template match="body"><身体><xsl:apply-templates select="body"></xsl:apply-templates></xsl:模板><xsl:template match="body"><body onLoad="ada" bgcolor="pink"></body></xsl:模板><!--删除匹配属性并完成工作--><xsl:template match="@attr"></xsl:模板><xsl:template match="输入"><xsl:元素名称="输入"><xsl:attribute name="type">submit</xsl:attribute><xsl:attribute name="value">完成</xsl:attribute><xsl:apply-templates/></xsl:element></xsl:模板></xsl:stylesheet>

解决方案

您的输入文档充满了格式错误,我不得不冒着猜测您意图的风险.请参阅下面的转换解决方案.我故意没有在您的评论ADD TABLE/TR/TD TAG"周围插入表格元素,因为这部分看起来太疯狂了,我在这里为您提供的任何解决方案都可能是对您所需规则的错误解释转型.

这个 XSLT 1.0 样式表 ...

<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:body"><element add="xyz" id="23"/><元素添加="xyz" id="24"/><body onLoad="ada" bgcolor="pink"><xsl:apply-templates select="@*|node()"/></xsl:模板><xsl:template match="xhtml:element1[@name='abc']/@src"><xsl:attribute name="src">xyz.jpg</xsl:attribute></xsl:模板><xsl:template match="xhtml:input[@id='1']"><表格名称=表格"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy><xsl:apply-templates select="following-sibling::xhtml:div[1]" mode="inside-form"/></表单></xsl:模板><xsl:template match="xhtml:div[previous-sibling::xhtml:*[1]/self::xhtml:input[@id='1']]"/><xsl:template match="xhtml:div" mode="inside-form"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy><input type="submit" value="Done"/></xsl:模板></xsl:stylesheet>

... 将把这个输入文件...

<头><meta http-equiv="Content-type" content="text/html; charset=utf-8"/><身体><div id="o"><div id="nd"><p>1</p>

<div class="TF" id="id12"><element1 name="abc" src="abc.jpg"/><input type="radio" id="1" event="xyz"/><div class="q"><br/><div id="ta3" class="block"><span style="a">ABC</span>

<br/>T<输入/>F<输入/><div id="sf"><div id="ta3">

</html>

...并产生这个输出文档...

<element add="xyz" id="23"/><元素添加="xyz" id="24"/><body onLoad="ada" bgcolor="pink"><div id="o"><div id="nd"><p>1</p>

<div class="TF" id="id12"><element1 name="abc" src="xyz.jpg"/><表格名称=表格"><input type="radio" id="1" event="xyz"/><div class="q"><br/><div id="ta3" class="block"><span style="a">ABC</span>

<br/>T <输入/>F <input/><div id="sf"><div id="ta3"/></div></div><input type="submit" value="Done"/></表单>

</html>

I want to change from one XML (XHTML) file to another using XSLT. In the new XML file I have to remove/add/modify some elements. So for that I created one identity.xsl file, which copies the entire source file and then I created a new XSLT which includes identity.xsl and then in that new XSLT I'm trying to do the modifications. I'm able to eliminate a few attributes, which are not required, by passing a template match which does nothing but I'm unable to add the new attributes in the existing tags and also unable to add the new elements at the specific location (with closing tags at particular location).

My Original file:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
</head>

<body>
  <div id="o">
    <div id="nd">
      <p>1</p>
    </div>

    <div class="TF id="id12">
      <element1 name="abc" src="abc.jpg"></script>
      <input type="radio" id="1" event="xyz">
      <div class="q">
        <br/>
        <div id="ta3" class="block">
          <span style="a">ABC</span>
        </div>
        <br/>T <input/> F <input/>
        <div id="sf">
          <div id="ta3">
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>

Required file:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
</head>

<!--HAVE TO AD THESE TWO ELEMENTS-->
<element add="xyz" id="23">
<element add="xyz" id="24">

<!--ADD ATTRIBUTES IN BODY TAG-->
<body onLoad="ada" bgcolor="pink">

  <div id="o">
    <div id="nd">
      <p>1</p>
    </div>

    <div class="TF id="id12">

      <!--HAVE TO UPATE THE VALUE OF SRC ATTRIBUTE -->
      <element1 name="abc" src="xyz.jpg"></script>

      <!--ADD THIS FORM ELEMENT WITH ATTRIBUTE-->
      <form name="form">
        <input type="radio" id="1" event="xyz">
        <div class="q">
          <br/>
          <div id="ta3" class="block">
            <span style="a">ABC</span>
          </div>

          <br/>T 
          <!--ADD TABLE/TR/TD TAG-->
          <table>
            <tr>
              <td>
                <input/>
              </td>
            </tr>
            <tr>
              </td>
              F <input/>
              </td>
            </tr>
          </table>

          <div id="sf">
            <div id="ta3">
            </div>
          </div>
        </div>

        <!--ADD INPUT TAG-->
        <input type="submit" value="Done"/>

      </div>
    </div>

    <!--CLOSE FORM TAG-->
  </form>
</div>
</body>
</html>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Import the identity transformation. -->
  <xsl:import href="identity.xsl"/>

  <xsl:template match="body">
    <body>
      <xsl:apply-templates select="body">
      </xsl:apply-templates>
    </body>
  </xsl:template>

  <xsl:template match="body">
    <body onLoad="ada" bgcolor="pink"></body>
  </xsl:template>

  <!--REMOVES THE MATCHING ATTRIBUTE and DOES THE JOB-->
  <xsl:template match="@attr"> </xsl:template>

  <xsl:template match="input">
    <xsl:element name="input">
      <xsl:attribute name="type">submit</xsl:attribute>
      <xsl:attribute name="value">Done</xsl:attribute>
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

解决方案

Your input document was so full of formation errors, I've had to take the risk of guessing your intentions. Please see the transform solution below. I deliberately did not include the insertion of the table elements around your comment "ADD TABLE/TR/TD TAG", as this section seemed so nutty that any solution that I provided for you here would likely be a wrong interpretation of your required rules of transformation.

This XSLT 1.0 style-sheet ...

<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:body">
  <element add="xyz" id="23" />
  <element add="xyz" id="24" />
 <body onLoad="ada" bgcolor="pink">
  <xsl:apply-templates select="@*|node()"/>
  </body>
</xsl:template>

<xsl:template match="xhtml:element1[@name='abc']/@src">
  <xsl:attribute name="src">xyz.jpg</xsl:attribute>  
</xsl:template>

<xsl:template match="xhtml:input[@id='1']">
  <form name="form">
   <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
   <xsl:apply-templates select="following-sibling::xhtml:div[1]" mode="inside-form"/> 
  </form>
</xsl:template>

<xsl:template match="xhtml:div[ preceding-sibling::xhtml:*[1]
   /self::xhtml:input[@id='1']]"/>

<xsl:template match="xhtml:div" mode="inside-form">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
 <input type="submit" value="Done"/> 
</xsl:template>

</xsl:stylesheet>

... will take this input document ...

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
</head>
<body>
 <div id="o">
  <div id="nd">
   <p>1</p>
  </div>
  <div class="TF" id="id12">
   <element1 name="abc" src="abc.jpg"/>
   <input type="radio" id="1" event="xyz"/>
   <div class="q">
    <br/>
    <div id="ta3" class="block">
     <span style="a">ABC</span>
    </div>
    <br/>T <input/> F <input/>
    <div id="sf">
     <div id="ta3">
     </div>
    </div>
   </div>
  </div>
 </div>
</body>
</html>

...and yield this output document ...

<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
  </head>
  <element add="xyz" id="23" />
  <element add="xyz" id="24" />
  <body onLoad="ada" bgcolor="pink">
    <div id="o">
      <div id="nd">
        <p>1</p>
      </div>
      <div class="TF" id="id12">
        <element1 name="abc" src="xyz.jpg" />
        <form name="form">
          <input type="radio" id="1" event="xyz" />
          <div class="q">
            <br />
            <div id="ta3" class="block">
              <span style="a">ABC</span>
            </div>
            <br />T <input /> F <input /><div id="sf"><div id="ta3" /></div></div>
          <input type="submit" value="Done" />
        </form>
      </div>
    </div>
  </body>
</html>

这篇关于XML to XML with XSLT - 添加、删除、修改元素和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆