xml使用xslt删除元素

阿米达D

我希望从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,它不起作用。有谁能够帮我?

马丁·霍恩(Martin Honnen)

该函数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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章