省略不包含特定子节点的节点的Parent节点

时间:2013-05-03 15:14:02

标签: xml xslt xpath

请帮助您关注xslt。在下面的xml中,如果具有data-type =“taxonomy”的ul节点不包含ul子节点,则必须在输出中省略该ul节点的父节点,其中属性id为“a12345600”的ul节点

<div class="css-treeview">                
 <ul id="a12345dem" data-id="a12345dem" data-type="taxonomy">  ## this node has to be omitted in the output##
    <li>
      <input type="checkbox" />
      <label>Sample Publications</label>
      <ul id="dc0d58dc" data-id="dc0d58dc" data-type="taxonomy"> ## here ul node with attribute has  data-type="taxonomy" no child ul node##
        <li>
          <input type="checkbox" />
          <label>Word</label>
        </li>
      </ul>    
    </li>
  </ul>
  <ul id="bcba11a9" data-id="bcba11a9" data-type="taxonomy">
    <li>
      <input type="checkbox" />
      <label>AfDB</label>
      <ul id="9cae7833" data-id="9cae7833" data-type="taxonomy">
        <li>
          <input type="checkbox" />
          <label>Tunis</label>
          <ul id="Content is the King sai ram" data-id="f02a0f2a" data-taxonomy="9cae7833">
            <li>
              <input type="checkbox" id="item-0" />
              <label for="item-0">
                <a class="lnkPub" data-pid="f02a0f2a" onclick="doContentLoad(this,'1_Front_Matter/publication.htm', '1', '1')" title="Content is the King sai ram">Content is the King sai ram</a>
              </label>          
            </li>
          </ul>        
        </li>
      </ul>    
      <ul id="Eine ganz normale Projektentwicklung for Demo" data-id="b1acb679" data-taxonomy="bcba11a9">
        <li>
          <input type="checkbox" id="item-0" />
          <label for="item-0">
            <a class="lnkPub" data-pid="b1acb679" onclick="doContentLoad(this,'1_Front_Matter/publication.htm', '1', '1')" title="Eine ganz normale Projektentwicklung for Demo">Eine ganz normale Projektentwicklung for Demo</a>
          </label>         
        </li>
      </ul>

    </li>
  </ul>
  <ul id="c8149f59" data-id="c8149f59" data-type="taxonomy">
    <li>
      <input type="checkbox" />
      <label>Siemens</label>
      <ul id="SCORM SCORM" data-id="79618046" data-taxonomy="bcba11a9">
        <li>
          <input type="checkbox" id="item-0" />
          <label for="item-0">
            <a class="lnkPub" data-pid="79618046" onclick="doContentLoad(this,'1_Front_Matter/publication.htm', '1', '1')" title="SCORM SCORM">SCORM SCORM</a>
          </label>         
        </li>
      </ul>
    </li>
  </ul>

  

XSLT

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE html></xsl:text>
    <html>
      <head>
        <title />
        <script src="Navigation.js">
        </script>
        <link rel="stylesheet" href="Navigation.css" />
        <link rel="stylesheet" href="treeview.css" />
        <style>nav#MainNav a {padding-top:10px !important; display: block}</style>
      </head>
      <body>
        <header class="navHeader">
          <nav>
            <a href="Taxonomy.htm">Taxonomy</a>
            <a href="Title.htm">Title</a>
            <a href="Date.htm">Date</a>
          </nav>
        </header>
        <nav id="MainNav">
         <div>
           <xsl:apply-templates select="@*" />
           <xsl:apply-templates select="ul">

           </xsl:apply-templates>
           </div>
        </nav>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="*|@*|text()">

    <xsl:copy>
      <xsl:apply-templates select="*|@*|text()" />
    </xsl:copy>

  </xsl:template>

  <xsl:template match="div">
    <xsl:copy>
      <xsl:apply-templates select="ul|@*|text()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="div/ul/li">
    <xsl:copy>
      <xsl:apply-templates select="*|@*|text()">
        <xsl:sort select="@data-title"/>

      </xsl:apply-templates>

    </xsl:copy>
  </xsl:template>
##xslt I used to remove the specific node##
  <xsl:template match="li[../@data-type='taxonomy']/label[not(following-sibling::ul)]">


  </xsl:template>

</xsl:transform>

预期产出。

 <div class="css-treeview">
  <ul id="bcba11a9" data-id="bcba11a9" data-type="taxonomy">
    <li>
      <input type="checkbox" />
      <label>AfDB</label>
      <ul id="9cae7833" data-id="9cae7833" data-type="taxonomy">
        <li>
          <input type="checkbox" />
          <label>Tunis</label>
          <ul id="Content is the King sai ram" data-id="f02a0f2a" data-taxonomy="9cae7833">
            <li>
              <input type="checkbox" id="item-0" />
              <label for="item-0">
                <a class="lnkPub" data-pid="f02a0f2a" onclick="doContentLoad(this,'1_Front_Matter/publication.htm', '1', '1')" title="Content is the King sai ram">Content is the King sai ram</a>
              </label>          
            </li>
          </ul>        
        </li>
      </ul>    
      <ul id="Eine ganz normale Projektentwicklung for Demo" data-id="b1acb679" data-taxonomy="bcba11a9">
        <li>
          <input type="checkbox" id="item-0" />
          <label for="item-0">
            <a class="lnkPub" data-pid="b1acb679" onclick="doContentLoad(this,'1_Front_Matter/publication.htm', '1', '1')" title="Eine ganz normale Projektentwicklung for Demo">Eine ganz normale Projektentwicklung for Demo</a>
          </label>         
        </li>
      </ul>

    </li>
  </ul>
  <ul id="c8149f59" data-id="c8149f59" data-type="taxonomy">
    <li>
      <input type="checkbox" />
      <label>Siemens</label>
      <ul id="SCORM SCORM" data-id="79618046" data-taxonomy="bcba11a9">
        <li>
          <input type="checkbox" id="item-0" />
          <label for="item-0">
            <a class="lnkPub" data-pid="79618046" onclick="doContentLoad(this,'1_Front_Matter/publication.htm', '1', '1')" title="SCORM SCORM">SCORM SCORM</a>
          </label>         
        </li>
      </ul>
    </li>
  </ul>
</div>

1 个答案:

答案 0 :(得分:1)

试试这个:

<xsl:template match="ul[descendant::ul[@data-type='taxonomy' and  not(descendant::ul) ] ]"/>

匹配(并忽略)ul与后代ul,属性数据类型值等于“taxonomy”并且没有后代ul。