Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XSLT将XML转换为XML-添加、删除、修改元素和属性_Xml_Xslt_Xhtml - Fatal编程技术网

使用XSLT将XML转换为XML-添加、删除、修改元素和属性

使用XSLT将XML转换为XML-添加、删除、修改元素和属性,xml,xslt,xhtml,Xml,Xslt,Xhtml,我想使用XSLT从一个XML(XHTML)文件更改为另一个。在新的XML文件中,我必须删除/添加/修改一些元素。为此,我创建了一个identity.xsl文件,它复制了整个源文件,然后我创建了一个新的XSLT,其中包括identity.xsl,然后在新的XSLT中我尝试进行修改。我可以通过传递模板匹配来消除一些不需要的属性,模板匹配不起任何作用,但我无法在现有标记中添加新属性,也无法在特定位置添加新元素(在特定位置使用结束标记) 我的原始文件: <?xml version="1.0" en

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

我的原始文件:

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

一,


就我个人而言,我无法忍受XSL-t。它太难阅读了


我的首选是为我想要生成的XML创建一个Velocity模板,并使用它将旧XML映射到新XML。它更容易可视化,也更有效。

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

此XSLT 1.0样式表…

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

xyz.jpg
。。。将接受此输入文档…

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

一,


基础知识
T F
…并生成此输出文档…

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

一,


基础知识
T F
能否更具体地说明您遇到的问题?@Michael我无法添加具有属性的新元素,也无法向现有元素添加新属性。但是,我可以删除/更新属性。例如,我必须在head标记之后添加两个元素,并向body标记添加新属性,但我不能这样做。例如,脚本标记是不平衡的。请更正,以便我们了解您的意图。RahuID,在我修复您的格式时,我可以清楚地告诉您,您的XML输入完全无效。也许你想修复它?谢谢你的回复。很抱歉,我是XSLT新手,所以我不知道XML的Velocity模板。你能建议我怎么做吗?检查超链接并开始阅读如何使用Velocity。“这相对容易。”肖恩德金非常感谢你的回答。我会按照你提到的方式做。再次感谢。非常感谢。它工作得很好。现在我可以尝试一些修改。:)我有个问题。就像上面的源XHTML中一样,有一个-,而在我完整的源文件中,我还有一个类似的,还有一个在“head”标记中,所以当我应用上面的XSLT时,它会更改source的值,并使其成为xyz.jpg,而我只需更改其中的src=“abc.jpg”。那么,你能告诉我,我如何才能做到这一点?谢谢你!我尝试过类似的方法,但没有成功,它删除了整个element1标记,其中src=“abc.jpg”Hi Rahul。请将您的问题作为另一个问题发布。我和其他人会看的。为了获得最佳结果,请提供示例输入、预期输出、XSLT版本(1.0或2.0)和转换规则。还要确保xml列表格式正确,没有错误。我期待着看到你的新问题。