XSLT:仅复制某些节点 [英] XSLT: copy certain nodes only

查看:222
本文介绍了XSLT:仅复制某些节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能告诉我如何使用XSLT执行此操作吗?



输入:



 code><?xml version =1.0?> 
< A>
< BB bb1 =bb1/>
< CC cc1 =cc1/>
< DD name =name1>
< EEE type=foovalue =50>
< FFFF id =id1>
< / EEE>
< / DD>
< DD name =name2>
< EEE type =barvalue =50>
< FFFF id =id2>
< / EEE>
< / DD>
< DD name =name3>
< EEE type=foovalue =40>
< FFFF id =id3>
< / EEE>
< / DD>





输出:



 <?xml version =1.0? 
< A>
< BB bb1 =bb1/>
< CC cc1 =cc1/>
< DD name =name3>
< EEE type =foovalue =40>
< FFFF id =id3>
< / EEE>
< / DD>
< DD name =name1>
< EEE type =foovalue =50>
< FFFF id =id1>
< / EEE>
< / DD>





复制所有除了如果它是一个DD,只有EEE / @ type =foo,并且所有的DD按EEE / @值排序。





XSLT:



pre> <?xml version =1.0?>
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output method =xmlindent =yes/>
< xsl:strip-space elements =*/>
< xsl:template match =@ * | node()>
< xsl:copy>
< xsl:apply-templates select =@ * | node()>
< xsl:sort select =EEE / @ type/>
< / xsl:apply-templates>
< / xsl:copy>
< / xsl:template>





真的只想保留那些EEE / @ type = foo的DD。



非常感谢。

解决方案

 <?xml version =1.0?& 
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output method =xmlindent =yes/>
< xsl:strip-space elements =*/>
< xsl:template match =@ * | node()>
< xsl:copy>
< xsl:apply-templates select =@ * | node()>
< xsl:sort select =EEE / @ type/>
< / xsl:apply-templates>
< / xsl:copy>
< / xsl:template>

您只需要添加一个模板

 <?xml version =1.0?> 
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output method =xmlindent =yes/>
< xsl:strip-space elements =*/>
< xsl:template match =@ * | node()>
< xsl:copy>
< xsl:apply-templates select =@ * | node()>
< xsl:sort select =EEE / @ type/>
< / xsl:apply-templates>
< / xsl:copy>
< / xsl:template>
< xsl:template match =DD [not(EEE / @ type ='foo')]/>

确保 DD c $ c> EEE / @ type 不等于'foo'不被复制。


Can you tell me how to peform this using XSLT ?

Input:

<?xml version="1.0"?>
<A>
  <BB bb1="bb1" />
  <CC cc1="cc1" />
  <DD name="name1">
    <EEE type="foo" value="50">
      <FFFF id="id1">
    </EEE>
  </DD>
  <DD name="name2">
    <EEE type="bar" value="50">
      <FFFF id="id2">
    </EEE>
  </DD>
  <DD name="name3">
    <EEE type="foo" value="40">
      <FFFF id="id3">
    </EEE>
  </DD>

Output:

<?xml version="1.0"?>
<A>
  <BB bb1="bb1" />
  <CC cc1="cc1" />
  <DD name="name3">
    <EEE type="foo" value="40">
      <FFFF id="id3">
    </EEE>
  </DD>
  <DD name="name1">
    <EEE type="foo" value="50">
      <FFFF id="id1">
    </EEE>
  </DD>

I.e. copy all except that if it is a DD, copy only if EEE/@type = "foo", and sort all DD by EEE/@value.

For now i've just found xsl code to copy everything and sort by say EEE/@type for example.

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()">
        <xsl:sort select="EEE/@type" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

That's already good enough but I really want to keep only those DD where EEE/@type = foo.

Thank You very much.

解决方案

To

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()">
        <xsl:sort select="EEE/@type" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

you just need to add a template

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()">
        <xsl:sort select="EEE/@type" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="DD[not(EEE/@type = 'foo')]"/>

that ensures those DD elements with EEE/@type not equal 'foo' are not copied.

这篇关于XSLT:仅复制某些节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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