2014-10-10 159 views
0

我希望從excel文件中刪除所有在Check標記中具有以數字開頭的值的元素。 這裏是我的XML文件格式:xml使用xslt刪除元素

<Scans> 
    <Results> 
     <Check>MS-123</Check> 
     <Result> 
      <Grade>Error</Grade> 
      <MachinesFound>0</MachinesFound> 
      <Machines></Machines> 
     </Result> 
     <Result> 
      <Grade>Critical</Grade> 
    </Result> 
    </Results> 
    <Results> 
     <Check>123</Check> 
     <Result> 
      <Grade>Error</Grade> 
     </Result> 
     <Result> 
      <Grade>Critical</Grade> 
     </Result> 
    </Results> 
    <Results> 
     <Check>456</Check> 
     <Result> 
      <Grade>Error</Grade> 
     </Result> 
     <Result> 
      <Grade>Critical</Grade> 
     </Result> 
    </Results> 
</Scans> 

我所有的XSLT代碼

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="#all" 
    version="2.0"> 

<xsl:template match="Scans"> 
    <HTML> 
    <STYLE> 
    BODY, TD, TH 
     { 
     font-family: Verdana; 
     font-size: 10pt; 
     text-align: left; 
     } 
    TH 
     { 
     background-color: #8CAAE6; 
     vertical-align: bottom; 
     } 
    .noBB 
     { 
     border-bottom-style: none; 
     } 
    .noTB 
     { 
     border-top-style: none; 
     vertical-align: top; 
     } 
    </STYLE> 
    <BODY> 
    <TABLE BORDER="1" BORDERCOLOR="gray" STYLE="border-collapse:collapse" CELLPADDING="2"> 
     <TR> 
      <TH> 
       Check 
      </TH> 
      <TH><FONT FACE="Wingdings 2" COLOR="red" SIZE="5">&#210;</FONT><BR/>Critical</TH> 
      <TH><FONT FACE="Wingdings 2" COLOR="red" SIZE="5">&#85;</FONT>         
      <BR/>Restart&#160;Required</TH> 
      <TH><FONT FACE="Wingdings 2" COLOR="yellow" SIZE="5">&#210;</FONT><BR/>Warning</TH> 
      <TH><FONT FACE="Wingdings" COLOR="green" SIZE="5">&#252;</FONT><BR/>Passed</TH> 
      <TH><FONT FACE="Wingdings 2" COLOR="blue" SIZE="5">&#234;</FONT> 
      <BR/>Not&#160;approved</TH> 
      <TH><FONT FACE="Wingdings 2" COLOR="blue" SIZE="5">&#222;</FONT><BR/>Note</TH> 
      <TH><FONT FACE="Verdana"  COLOR="red" SIZE="5">!</FONT><BR/>Error</TH> 
      <TH><FONT FACE="Webdings" COLOR="blue" SIZE="5">i</FONT><BR/>Informational</TH> 
      <TH><FONT FACE="Webdings" COLOR="blue" SIZE="5">&#120;</FONT>    

      <BR/>Not&#160;performed</TH> 
      <TH>Total</TH> 
     </TR> 
     <xsl:apply-templates select="Results"/> 
    </TABLE> 
    </BODY> 
    </HTML> 
</xsl:template> 

<xsl:template match="Results"> 
    <TR> 
     <TH VALIGN="TOP" ALIGN="LEFT" CLASS="noBB"> 
       <B><xsl:value-of select="Check"/></B> 
       <BR/> 
       <xsl:if test = "KBID != ''"> 
       <I>KB:<xsl:value-of select="KBID"/></I> 
       <BR/> 
       </xsl:if> 
      <BUTTON> 
       <xsl:attribute name="ID">R1<xsl:value-of select="Check"/><xsl:value-of select="KBID"/></xsl:attribute> 
       <xsl:attribute name="ONCLICK"> 
        document.all["R1<xsl:value-of select="Check"/><xsl:value-of select="KBID"/>"].style.display = 'none'; 
     document.all["R2<xsl:value-of select="Check"/><xsl:value-of select="KBID"/>"].style.display = ''; 
       </xsl:attribute> 
       &gt;&gt; 
      </BUTTON> 
     </TH> 

     <xsl:apply-templates mode="row2" select="Result[Grade='Critical']" /> 
     <xsl:apply-templates mode="row2" select="Result[Grade='RestartRequired']" /> 
     <xsl:apply-templates mode="row2" select="Result[Grade='Warning']" /> 
     <xsl:apply-templates mode="row2" select="Result[Grade='Passed']" /> 
     <xsl:apply-templates mode="row2" select="Result[Grade='NotApproved']" /> 
     <xsl:apply-templates mode="row2" select="Result[Grade='Note']" /> 
     <xsl:apply-templates mode="row2" select="Result[Grade='Error']" /> 
     <xsl:apply-templates mode="row2" select="Result[Grade='Info']" /> 
     <xsl:apply-templates mode="row2" select="Result[Grade='NotPerformed']" /> 

     <TD VALIGN="TOP" ALIGN="RIGHT" CLASS="noBB"> 
      <B> <xsl:value-of select="count(Result/Machines/Machine)"/> </B> 
     </TD> 
    </TR> 
    <TR STYLE="display:none"> 
     <xsl:attribute name="ID">R2<xsl:value-of select="Check"/><xsl:value-of select="KBID"/> 
     </xsl:attribute> 
    <TH CLASS="noTB"> 
    <BUTTON> 
     <xsl:attribute name="ONCLICK"> 
       document.all["R1<xsl:value-of select="Check"/><xsl:value-of select="KBID"/>"].style.display = ''; 
       document.all["R2<xsl:value-of select="Check"/><xsl:value-of select="KBID"/>"].style.display = 'none'; 
      </xsl:attribute> 
       &lt;&lt; 
      </BUTTON> 
     </TH> 

     <xsl:apply-templates mode="row3" select="Result[Grade='Critical']" /> 
     <xsl:apply-templates mode="row3" select="Result[Grade='RestartRequired']" /> 
     <xsl:apply-templates mode="row3" select="Result[Grade='Warning']" /> 
     <xsl:apply-templates mode="row3" select="Result[Grade='Passed']" /> 
     <xsl:apply-templates mode="row3" select="Result[Grade='NotApproved']" /> 
     <xsl:apply-templates mode="row3" select="Result[Grade='Note']" /> 
     <xsl:apply-templates mode="row3" select="Result[Grade='Error']" /> 
     <xsl:apply-templates mode="row3" select="Result[Grade='Info']" /> 
     <xsl:apply-templates mode="row3" select="Result[Grade='NotPerformed']" /> 

     <TD CLASS="noTB">&#160;</TD> 
    </TR> 
</xsl:template> 



<xsl:template match="Result" mode="row2"> 
    <xsl:variable name="Count" select="count(Machines/Machine)" /> 
    <TD VALIGN="TOP"> 
     <xsl:if test="$Count != 0"> 
      <B> <xsl:value-of select="$Count"/> </B> 
      (<xsl:value-of select="round (100 * $Count div   count(parent::Results/Result/Machines/Machine))"/>%) 
     </xsl:if> 
    </TD> 
    </xsl:template> 

    <xsl:template match="Result" mode="row3"> 
    <TD VALIGN="TOP"> 
     <xsl:apply-templates select="Machines/Machine" /> 
    </TD> 
    </xsl:template> 

    <xsl:template match="Machine"> 
    <A HREF="{@path}"> 
    <NOBR><xsl:value-of select="."/></NOBR> 
    </A> 
    <BR/> 
</xsl:template> 


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


<xsl:template match="Results[Check[matches(., '^[0-9]')]]"/> 


</xsl:stylesheet> 


I need to get rid of elements like 
<!--<Results> 
    <Check>123</Check> 
    <Result> 
     <Grade>Error</Grade> 
    </Result> 
    <Result> 
     <Grade>Critical</Grade> 
    </Result> 
</Results> --> 

用我的XSLT這是行不通的。 任何人都可以幫助我嗎?

回答

1

函數starts-with使用純字符串,而不是正則表達式。如果您使用XSLT 2.0處理器,則可以輕鬆使用matches,例如

<xsl:template match="Results[Check[matches(., '^[0-9]')]]"/> 

隨着XSLT 1.0它可能足以使用

<xsl:template match="Results[Check[translate(substring(., 1, 1), '', '') = '']]"/> 

一個完整的樣品是

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="#all" 
    version="2.0"> 

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

<xsl:template match="Results[Check[matches(., '^[0-9]')]]"/> 

</xsl:stylesheet> 

,當與撒克遜9.5 HE,執行輸出

<?xml version="1.0" encoding="UTF-8"?><Scans> 
    <Results> 
     <Check>MS-123</Check> 
     <Result> 
      <Grade>Error</Grade> 
     </Result> 
     <Result> 
      <Grade>Critical</Grade> 
    </Result> 
    </Results> 
+0

它不起作用。我不知道爲什麼。我在這些行之前應用其他xsl:模板,因爲我想將它看作一個很好的表格。這可能是因爲我沒有把這些代碼放在正確的地方?我把這段代碼放到了doc的末尾。我真的不明白,爲什麼它不起作用:( – 2014-10-10 12:39:18

+0

我已經編輯並提供了一個完整的樣本及其輸出的樣本,如果你仍然有問題,那麼編輯你的問題的必要細節,或者用新的細節開始一個新的問題,特別是如果你有其他的目的而不是刪除一個節點 – 2014-10-10 13:02:11

+0

謝謝Martin,我嘗試了所有,但它是一樣的。數字仍然存在,我將使用所有xslt文件編輯我的問題,再次感謝您 – 2014-10-10 14:14:40