2012-12-21 251 views
0

我試圖刪除我的XML文檔中的空元素。我想在id31,id32和id33爲空時將其刪除。以下是XSLT輸出的示例。我正在嘗試使用下面的XSLT,但我沒有任何運氣。關於我在做什麼的任何想法都是錯誤的?由於XSLT刪除空白元素

數據

<Item type="Part AML"> 
    <related_id> 
     <Item type="Manufacturer Part"> 
     <id keyed_name="1234" type="Manufacturer Part"></id> 
     <manufacturer keyed_name="Bobs Workshop" type="Manufacturer"></manufacturer> 
     <item_number>1234</item_number> 
     </Item> 
    </related_id> 
    </Item> 
    <Item type="Part Document"> 
    <related_id keyed_name="D000005" type="Document"> 
     <Item type="Document"> 
     <id keyed_name="D000005" type="Document"></id> 
     <major_rev>B</major_rev> 
     <name>Firmware 8-16-12</name> 
     <item_number>D000005</item_number> 
     </Item> 
    </related_id> 
    </Item> 
    <Item type="Part Document"> 
    <related_id keyed_name="D000003" type="Document"> 
     <Item type="Document"> 
     <id keyed_name="D000003" type="Document"></id> 
     <major_rev>A</major_rev> 
     <name>Design Spec</name> 
     <item_number>D000003</item_number> 
     </Item> 
    </related_id> 
    </Item> 

輸出

<td width="5%" align="center" uniqueID="ms__id31"></td> 
<td width="13%" align="center" uniqueID="ms__id32"></td> 
<td width="13%" align="center" uniqueID="ms__id33"></td> 
<td width="5%" align="center" uniqueID="ms__id34">D000003</td> 
<td width="13%" align="center" uniqueID="ms__id35">Design Spec</td> 
<td width="8%" align="center" uniqueID="ms__id36">A</td> 

代碼從論壇

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

<xsl:template match="*[not(@*|*|comment()|processing-instruction()) and normalize-space()='']"/> 

我的XSLT

<xsl:template match="Item[@type='Part AML']|Item[@type='Part Document']"> 
<tr> 
    <td width="5%" align="center" uniqueID="ms__id31"> 
    <xsl:value-of select="state"/> 
    </td> 
    <td width="13%" align="center" uniqueID="ms__id32"> 
    <xsl:value-of select="related_id[@type='Manufacturer Part']/Item/manufacturer/@keyed_name"/> 
    </td> 
    <td width="13%" align="center" uniqueID="ms__id33"> 
    <xsl:value-of select="related_id[@type='Manufacturer Part']/Item/item_number"></xsl:value-of> 
    </td> 
    <td width="13%" align="center" uniqueID="ms__id34"> 
     <xsl:value-of select="related_id[@type='Document']/Item/item_number"></xsl:value-of> 
    </td> 
    <td width="13%" align="center" uniqueID="ms__id35"> 
     <xsl:value-of select="related_id[@type='Document']/Item/name"/> 
    </td> 
    <td width="8%" align="center" uniqueID="ms__id36"> 
     <xsl:value-of select="related_id[@type='Document']/Item/major_rev"/> 
    </td> 
</tr> 

+0

user1618044,您尚未提供任何源XML文檔。我認爲不需要的輸出是您的源XML文檔。請編輯問題並提供源XML文檔,以便人們不需要猜測需要的代碼。 –

回答

1

使用模板。這樣一來,沒有一個是在空的情況下應用:

<xsl:template match="Item[@type='Part AML']|Item[@type='Part Document']"> 
<tr> 
    <xsl:apply-templates select="state"/> 

    <xsl:apply-templates select="related_id[@type='Manufacturer Part']/Item/manufacturer/@keyed_name"/> 

    <xsl:apply-templates select="related_id[@type='Manufacturer Part']/Item/item_number"> 

    <td width="13%" align="center" uniqueID="ms__id34"> 
     <xsl:value-of select="related_id[@type='Document']/Item/item_number"></xsl:value-of> 
    </td> 
    <td width="13%" align="center" uniqueID="ms__id35"> 
     <xsl:value-of select="related_id[@type='Document']/Item/name"/> 
    </td> 
    <td width="8%" align="center" uniqueID="ms__id36"> 
     <xsl:value-of select="related_id[@type='Document']/Item/major_rev"/> 
    </td> 
</tr> 
</xsl:template> 

<xsl:template match="Item[@type='Part AML' or @type='Part Document']/state"> 
    <td width="5%" align="center" uniqueID="ms__id31"> 
    <xsl:apply-templates> 
    </td> 
</xsl:template> 

<xsl:template match="Item[@type='Part AML' or @type='Part Document']/related_id[@type='Manufacturer Part']/Item/manufacturer/@keyed_name"> 
    <td width="13%" align="center" uniqueID="ms__id32"> 
    <xsl:apply-templates> 
    </td> 
</xsl:template> 

<xsl:template match="Item[@type='Part AML' or @type='Part Document']/related_id[@type='Manufacturer Part']/Item/item_number"> 
    <td width="13%" align="center" uniqueID="ms__id33"> 
    <xsl:apply-templates> 
    </td> 
</xsl:template> 

您可以使用相同的模式對其餘<td>■如果他們有可能爲空值了。

+0

非常感謝。這照顧到了這個問題。 – user1618044