xml使用xslt删除元素

时间:2014-10-10 10:16:19

标签: xml excel 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">&#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 个答案:

答案 0 :(得分: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), '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>