xml使用xslt删除元素

我希望从Excel文件中删除所有在Check标签中具有以数字开头的值的元素。 这是我的XML文件格式:

<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">Ò</FONT><BR/>Critical</TH> <TH><FONT FACE="Wingdings 2" COLOR="red" SIZE="5">U</FONT> <BR/>Restart Required</TH> <TH><FONT FACE="Wingdings 2" COLOR="yellow" SIZE="5">Ò</FONT><BR/>Warning</TH> <TH><FONT FACE="Wingdings" COLOR="green" SIZE="5">ü</FONT><BR/>Passed</TH> <TH><FONT FACE="Wingdings 2" COLOR="blue" SIZE="5">ê</FONT> <BR/>Not approved</TH> <TH><FONT FACE="Wingdings 2" COLOR="blue" SIZE="5">Þ</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">x</FONT> <BR/>Not 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"> </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是行不通的。 有谁能够帮助我?

函数starts-with纯string,而不是正则expression式。 如果您使用XSLT 2.0处理器,则可以轻松地使用matches例如

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

使用XSLT 1.0就足够了

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

一个完整的样本是

 <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> 

当用Saxon 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>