使用XSLT将XML转换为其他XML并删除空标签

时间:2018-07-17 18:31:43

标签: xml xslt

慢慢学习XSLT编码。需要帮助/建议。 在这里,我需要通过读取XML node1使用XSLT使用newnode1生成xml,并且还从输出XML中删除任何空标记(如果有的话)的情况。这是我的xml的样子。

<div class="popup hide" id="popup">
  <div class="pmenu" id="popup_trigger">
    <p>GET THE<br/>BOOK</p>
    <img src="del.png">
  </div>
  <div class="pbody">
    <h4>HOW TO GET THE PHONEBOOK</h4>
    <p>Books were delivered to every house in Lakewood and surrounding areas during the month of February. </p>
    <h5>If you did not receive one after this time you may pick one up at either one of the Wine on 9 locations.</h5>
    <div class="address">
      <div class="aicon">
        <i class="fa fa-map-marker"></i>
      </div>
      <div class="aadd">
        <h6><b>North location</b><br/> 6545 Rt 9 North, Howell, NJ 07731</h6>
      </div>
      <div class="aicon">
        <i class="fa fa-map-marker"></i>
      </div>
      <div class="aadd">
        <h6><b>South location</b><br/> 945 River Ave, Lakewood, NJ 08701</h6>
      </div>
    </div>
  </div>
</div>

这就是我现在的XSLT代码的样子

 <?xml version="1.0" encoding="UTF-8"?>
 <Node>
   <Node_1>
     <Line>1</Line>
     <Text>First node1</Text>
     <Desc>Desc1</Desc>
     <Cust>Cust1</Cust>
     <Value1/>
 </Node_1>
 <Node_1>
     <Line>2</Line>
     <Text>First node2</Text>
     <Desc>Desc2</Desc>
     <Cust>Cust2</Cust>
     <Value1></Value1>
 </Node_1>
 <Node_2>
     <Line>1</Line>
     <ReadInd>Y</ReadInd>
     <WriteInd>Y</WriteInd>
     <UpdateInd>Y</UpdateInd>
     <Value2/>
 </Node_2>
 <Node_2>
     <Line>2</Line>
     <ReadInd>N</ReadInd>
     <WriteInd>N</WriteInd>
     <UpdateInd>N</UpdateInd>
     <Value2/>
 </Node_2>
 </Node>              

我还有另一个XSLT代码可以删除空标签,

 <xsl:stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
 <xsl:output method="xml" indent="yes" />
 <xsl:strip-space elements="*"/>
  <xsl:key name="node2" match="Node_2" use="Line" />

  <xsl:template match="/">   
   <xsl:for-each select="Node/Node_1">
    <node>
    <line><xsl:value-of select="Line"/></line>
    <text><xsl:value-of select="Text"/></text>
    <desc><xsl:value-of select="Desc"/></desc>
    <cust><xsl:value-of select="Cust"/></cust>
    <val1><xsl:value-of select="value1"/></val1>
    <xsl:for-each select="key('node2', Line)">
        <subnode>
        <readind><xsl:value-of select="ReadInd"/></readind>
        <writeind><xsl:value-of select="WriteInd"/></writeind>
        <updateind><xsl:value-of select="UpdateInd"/></updateind>
        <val2><xsl:value-of select="value2"/></val2>
        </subnode>
    </xsl:for-each>
    </node>
    </xsl:for-each>
    </xsl:template>    
    </xsl:stylesheet> 

有没有一种方法可以将XML转换为XML,还可以在单​​个XSLT代码中删除空标记。

1 个答案:

答案 0 :(得分:0)

考虑一种动态解决方案,而无需写出所有节点名称和条件xpath查询以使用[text()!='']过滤出空文本:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>
  <xsl:key name="node_key" match="Node/*" use="Line"/>

  <xsl:template match="/Node">
    <nodes>
        <xsl:apply-templates select="*[generate-id() = generate-id(key('node_key', Line)[1])]"/>
    </nodes>
  </xsl:template>

  <xsl:template match="Node/*">
    <xsl:variable name="curr_line" select="Line" />
    <node>        
        <xsl:copy-of select="*[text()!='']"/>       
        <subnode>
            <xsl:copy-of select="following-sibling::Node_2[Line = $curr_line]/*[text()!='']"/>
        </subnode>
    </node>
  </xsl:template>

</xsl:stylesheet>

Demo Fiddle