Xsl转换,删除/移动图像标记

时间:2016-06-01 15:02:26

标签: html xml xslt xslt-1.0 xslt-2.0

我有这种格式的XHtml文档。如何使用XSL在图像div中移动所有img标签。

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  <title>Title</title>
  <link href="css/style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <div class="article">
    <div class="docTitles">
      <h1 class="title">doc title</h1>
    </div>
    <div class="section">
      <h2 class="generic">subtitle</h2>
      <p>
        <img alt="Image" src="images/{D11F8168-E415-498A-BC1C-6FEF3514B120}.JPG" width="200" />paragraph</p>
    </div>
  </div>
  <div id="images"></div>
</body>

</html>

这是我正在寻找的输出,所有图像标签都在图像div中移动

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  <title>Title</title>
  <link href="css/style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <div class="article">
    <div class="docTitles">
      <h1 class="title">doc title</h1>
    </div>
    <div class="section">
      <h2 class="generic">subtitle</h2>
      <p>paragraph</p>
    </div>
  </div>
  <div id="images">
    <img alt="Image" src="images/{D11F8168-E415-498A-BC1C-6FEF3514B120}.JPG" width="200" />
  </div>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

尝试:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="x:img"/>

<xsl:template match="x:div[@id='images']">
    <xsl:copy>
        <xsl:copy-of select="//x:img"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>