如果子节点不存在,如何通过应用XSL从XML删除父标记 [英] How to remove parent tag from XML by applying XSL if child node does not exists

查看:125
本文介绍了如果子节点不存在,如何通过应用XSL从XML删除父标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有一个问题与通过应用xsl在创建xml文件中删除标签有关.

例如:

Hello All

I have one issue related to removing tag from xml file while creating it by applying xsl.

e.g:

<!--START: operation-->
<xsl:element name="operation">
<xsl:if test="string-length(here value of a1 from another xml) > 0">
    <xsl:element name="a1">
       <xsl:value-of select="normalize-space(here value of a1 from another xml); />
    </xsl:element>
</xsl:if>
<xsl:if test="string-length(here value of a1 from another xml) > 0">
       <xsl:element name="a1">
            <xsl:value-of select="normalize-space(here value of a1 from another xml); />
    </xsl:element>
</xsl:if>
<xsl:if test="string-length(here value of a1 from another xml) > 0">
    <xsl:element name="a1">
       <xsl:value-of select="normalize-space(here value of a1 from another xml); />
    </xsl:element>
</xsl:if>
    <!--END: a1-->
</xsl:element>
<!--END: operation-->



有多个带有多个计数的子标签,例如a1,b1,c1,d1 [如示例a1所示],其值来自另一个父XML {其标签和名称不同}.仅当另一个XML(即父XML)中的值不为null时,才创建子元素.
我的问题是,如果它的子节点不存在,我想删除该操作标签.
注意:在这种情况下,我不想应用其他XSL来删除操作标签.当我通过在output.xml上应用另一个XSL来创建示例应用程序时,它运行正常.
该代码用于在子节点不存在的情况下删除操作标签,如下所示在另一个xsl
中使用



There are number of child tags such as a1,b1,c1,d1 with multiple counts[as shown in example a1] whose value is coming from another Parent XML{whose tags and names are different}. The child elements are created only when the value in another XML i.e. Parent XML is not null.
My issue is i want to remove the operation tag if its child node does not exists.
Note: In this i don''t want to apply another XSL for removing the operation tag. As i created the sample application by applying another XSL on the output.xml which is working fine.
the code is use to remove the operation tag if child node does not exists is shown below used in another xsl

<xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="operation[not(a1or b1 or c1 or d1)]"/>


有什么方法可以通过仅应用一个XSL来删除标签.

感谢您提前提供帮助并等待您的建议.


Is there any way by which i can remove the tag by applying only one XSL.

Thanks for helping in advance and waiting for your suggestion.

推荐答案

尝试以下操作:

Try this:

<xsl:template match="text()">
    <xsl:variable name="Nod" >
       <xsl:value-of select="local-name(..)" />
    </xsl:variable>
    <xsl:variable name="NodValue" >
       <xsl:value-of select="." />
    </xsl:variable>
 <xsl:if test="(contains(


Nod,'a1')或包含(
Nod,'a1') or contains(


Nod,'b1')或包含(
Nod,'b1') or contains(


这篇关于如果子节点不存在,如何通过应用XSL从XML删除父标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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