使用 XSLT 跳过节点 [英] skip over nodes with XSLT

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

问题描述

是否可以在处理 xml 文件时跳过节点?例如:假设我有以下 xml 代码:

Is it possible to skip over nodes while processing a xml file? For example: say I have the following xml code:

<mycase desc="">
  <caseid> id_1234 </caseid>
  <serid ref=""/>    
  ......
  ......
  ......  
</mycase>

我想让它看起来像这样:

and I want to make it look like this:

<mycase desc="" caseid="id_1234">
 .....
 .....
</mycase>

目前我正在这样做:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" exclude-result-prefixes="xs xdt err fn"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:fn="http://www.w3.org/2005/xpath-functions"
            xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
            xmlns:err="http://www.w3.org/2005/xqt-errors">

          <xsl:output method="xml" indent="yes"/>
          <xsl:template match="/">
            <xsl:apply-templates/> 
          </xsl:template>

         <xsl:template match="mycase">          
            <xsl:element name="mycase">
               <xsl:attribute name="desc"/>
               <xsl:attribute name="caseid">
                 <xsl:value-of select="caseid"/>
               </xsl:attribute>
              <xsl:apply-templates/>
            </xsl:element>
         </xsl:template>
         ......
         ......

这确实创建了我想要的,但由于 它处理所有节点.虽然我希望它跳过处理 caseid 和 serid 一起.这也适用于其他节点,这些节点在新的 XML 结构中将不可用.那么如何跳过不想使用 xslt 处理的节点.

This does create what I want it to but because of <xsl:apply-templates/> it processes all the node. while I want it to skip processing caseid and serid all together. This also applys for other nodes, which will not be available in the new XML structure. So how can I skip the nodes that I don't want to process using xslt.

推荐答案

您可以使用空模板来抑制输入文档中某些节点的输出:

You can use empty templates to suppress output of certain nodes in your input document:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="mycase">
    <mycase caseid="{caseid}">
      <xsl:apply-templates select="@*|node()"/>
    </mycase>
  </xsl:template>

  <xsl:template match="caseid|serid"/>

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

</xsl:stylesheet>

这篇关于使用 XSLT 跳过节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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