如何使用Xslt从Xml中删除节点 [英] How to remove a node from Xml using Xslt

查看:349
本文介绍了如何使用Xslt从Xml中删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Xslt,如何删除< Date>来自以下Xml的节点:


< TheXml>

  < ThePhrases>

  &NBSP; <短语>

  &NBSP; &NBSP; < PhraseCode> test< / PhraseCode>

  &NBSP; &NBSP; < PhraseData File =" text.xml" QC ="1">

  &NBSP; &NBSP; &NBSP; <金额> $ 113,110.00< /金额>

  &NBSP; &NBSP; &NBSP; < Date> 4-22-1985< / Date>

  &NBSP; &NBSP; < / PhraseData>

  &NBSP; < / Phrase>

  < / ThePhrases>

< / TheXml>



我试图得到以下结果:


< TheXml>

  < ThePhrases>

  &NBSP; <短语>

  &NBSP; &NBSP; < PhraseCode> test< / PhraseCode>

  &NBSP; &NBSP; < PhraseData File =" text.xml" QC ="1">

  &NBSP; &NBSP; &NBSP; <金额> $ 113,110.00< /金额>

  &NBSP; &NBSP; < / PhraseData>

  &NBSP; < / Phrase>

  < / ThePhrases>

< / TheXml>


解决方案

这就像

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

< xsl:template match =" PhraseData / Date" />


 

这将确保任何"日期"。任何"PhraseData"的子元素。元素被删除,当然根据您的需要,你可以收紧条件,例如。


  < xsl:template match =" PhraseData [@File ='text.xml'] / Date" />




Using Xslt, how can I remove the <Date> node from the Xml below:

<TheXml>
  <ThePhrases>
    <Phrase>
      <PhraseCode>test</PhraseCode>
      <PhraseData File="text.xml" QC="1">
        <Amount>$113,110.00</Amount>
        <Date>4-22-1985</Date>
      </PhraseData>
    </Phrase>
  </ThePhrases>
</TheXml>

I am trying to get the below result:

<TheXml>
  <ThePhrases>
    <Phrase>
      <PhraseCode>test</PhraseCode>
      <PhraseData File="text.xml" QC="1">
        <Amount>$113,110.00</Amount>
      </PhraseData>
    </Phrase>
  </ThePhrases>
</TheXml>

解决方案

That is as easy as

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

<xsl:template match="PhraseData/Date"/>

 

That would make sure any "Date" child elements of any "PhraseData" elements are removed, of course depending on your needs you can tighten the conditon as with e.g.

  <xsl:template match="PhraseData[@File = 'text.xml']/Date"/>


这篇关于如何使用Xslt从Xml中删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆