XSLT将具有父节点的节点移动到具有给定属性的每个父节点的兄弟节点中

时间:2016-06-09 16:25:05

标签: xslt

我有以下来源a.html:

interface_master(handles)

我想:

  1. 在每个div class =“b”里面复制元素div class =“x”后自己的文本和任何div class =“b1”
  2. 从div class =“a”
  3. 中删除原来的div class =“x”

    输出应为:

    <html>
      <head>
        <title>title</title>
      </head>
      <body>
        <div class="a">a
          <div class="a1">a1</div>
          <div class="x" type="typea2">x
            <div class="x1">x1</div>
          </div>
          <div class="a3">a3</div>
        </div>
        <div class="b">b_1
          <div class="b1">b1_1</div>
          <div class="b3">b3_11</div>
          <div class="b3">b3_12</div>
          <div class="b3">b3_13</div>
        </div>
        <div class="b">b_2
          <div class="b1">b1_2</div>
          <div class="b3">b3_21</div>
          <div class="b3">b3_22</div>
        <div class="b">b_3
          <div class="b1">b1_3</div>
          <div class="b3">b3_31</div>
          <div class="b3">b3_32</div>
          <div class="b3">b3_33</div>
          <div class="b3">b3_34</div>
        </div>
      </body>
    </html>
    

    我使用了以下shell脚本a.sh:

    <html>
      <head>
        <title>title</title>
      </head>
      <body>
        <div class="a">a
          <div class="a1">a1</div>
          <div class="a3">a3</div>
        </div>
        <div class="b">b_1
          <div class="b1">b1_1</div>
          <div class="x" type="typea2">x
            <div class="x1">x1</div>
          </div>
          <div class="b3">b3_11</div>
          <div class="b3">b3_12</div>
          <div class="b3">b3_13</div>
        </div>
        <div class="b">b_2
          <div class="b1">b1_2</div>
          <div class="x" type="typea2">x
            <div class="x1">x1</div>
          </div>
          <div class="b3">b3_21</div>
          <div class="b3">b3_22</div>
        <div class="b">b_3
          <div class="b1">b1_3</div>
          <div class="x" type="typea2">x
            <div class="x1">x1</div>
          </div>
          <div class="b3">b3_31</div>
          <div class="b3">b3_32</div>
          <div class="b3">b3_33</div>
          <div class="b3">b3_34</div>
        </div>
      </body>
    </html>
    

    使用以下a.xslt:

    xsltproc --html a.xslt a.html > b.html
    

    但是我没有得到理想的结果?

    请问我的帮助吗?

1 个答案:

答案 0 :(得分:2)

如果内部div始终存在,我会为它编写一个模板:

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

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

 <!-- ignore x-inside-a -->
 <xsl:template match="div[@class='a']/div[@class='x']"/>

 <!-- place the x-inside-a into each b after b1 -->
 <xsl:template match="div[@class='b']/div[@class = 'b1']">
  <xsl:call-template name="identity"/>
  <xsl:copy-of select="//div[@class='a']/div[@class='x']" />
 </xsl:template>

</xsl:stylesheet>