2011-07-03 35 views
4

我有一个xml如下。XSLT null比较

<attributes> 
     <attribute> 
      <attributeName>agenda-group</attributeName> 
      <value>generic</value> 
     </attribute> 
     <attribute> 
      <attributeName>auto-focus</attributeName> 
      <value>true</value> 
     </attribute> 
     <attribute> 
      <attributeName>no-loop</attributeName> 
      <value>true</value> 
     </attribute> 
     <attribute> 
      <attributeName>salience</attributeName> 
      <value>73</value> 
     </attribute> 
    </attributes> 

当我得到上面的块i需要上述嵌段复制,因为它是在所得xml.If我得到的下面与块超时值

​​

我需要省略此块中我的结果xml.I使用xslt进行翻译。 请提供一些指针来获得所需的输出。

回答

1

使用identity template并添加这些模板:

<xsl:template match="attributes[not(attribute/value/text())]" /> 
<xsl:template match="attribute[not(value/text())]" /> 

这两个空模板赶上没有任何价值,并没有产生输出为他们<attributes><attribute>元素,有效地删除它们。

-1

试试这个:

<xsl:for-each select="//attributes[descendant::attribute]"> 
    some stuff 
</xsl:for-each> 
0

这种转变

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match= 
"attributes[not(node())] 
| 
    attribute[not(attributeName/text())] 
"/> 
</xsl:stylesheet> 

当这个XML文档施加(注意最后的空<attributes>attribute/attributeName):

<attributes> 
    <attribute> 
     <attributeName>agenda-group</attributeName> 
     <value>generic</value> 
    </attribute> 
    <attribute> 
     <attributeName>auto-focus</attributeName> 
     <value>true</value> 
    </attribute> 
    <attribute> 
     <attributeName>no-loop</attributeName> 
     <value>true</value> 
    </attribute> 
    <attribute> 
     <attributeName>salience</attributeName> 
     <value>73</value> 
    </attribute> 

    <attribute> 
     <attributeName></attributeName> 
     <value></value> 
    </attribute> 

    <attributes/> 
</attributes> 

产生想要的结果(空元素忽略 - 不复制):

<attributes> 
    <attribute> 
    <attributeName>agenda-group</attributeName> 
    <value>generic</value> 
    </attribute> 
    <attribute> 
    <attributeName>auto-focus</attributeName> 
    <value>true</value> 
    </attribute> 
    <attribute> 
    <attributeName>no-loop</attributeName> 
    <value>true</value> 
    </attribute> 
    <attribute> 
    <attributeName>salience</attributeName> 
    <value>73</value> 
    </attribute> 
</attributes> 

说明:身份规则(即副本每个节点“原样”)是由一个单一的模板重写该匹配想要的“空”元素并且没有主体,所以他们被简单地忽略/忽略。

+0

我不知道由OP所需的输入文件是为你承担。请参阅我的解答。 –

+0

@empo:是的,这是我对这个问题的解释。如果值为空/缺少,名称 - 值对仍然有意义。 –

0

以下变换将任何attributes在输出复制,通过省略:

  • attributes
  • attributes只有空attribute孩子(或空的子元素)
  • 任何空的儿童attribute(或带有空的子元素)

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="attributes[not(*)] 
    | 
    attributes[count(*)=count(attribute[.=''])] 
    | 
    attribute[.='']"/> 

</xsl:stylesheet> 

当在接下来的输入应用:

<root> 
    <attributes> 
     <attribute> 
      <attributeName>agenda-group</attributeName> 
      <value>generic</value> 
     </attribute> 
     <attribute/> 
     <attribute> 
      <attributeName></attributeName> 
      <value></value> 
     </attribute> 
     <attribute> 
      <attributeName>salience</attributeName> 
      <value>73</value> 
     </attribute> 
    </attributes> 
    <attributes> 
     <attribute> 
      <attributeName></attributeName> 
      <value></value> 
     </attribute> 
    </attributes> 
    <attributes/> 
</root> 

生产:

<root> 
    <attributes> 
     <attribute> 
     <attributeName>agenda-group</attributeName> 
     <value>generic</value> 
     </attribute> 
     <attribute> 
     <attributeName>salience</attributeName> 
     <value>73</value> 
     </attribute> 
    </attributes> 
</root> 
+0

我需要基于属性标记来验证xml。如果属性标记为空,我需要在运行时使用java生成异常。如何实现相同。请给我一些指点。 – thogadam

+0

@thog为什么没有用java标记问题,还添加了这个细节? –

+0

@thog在你的java应用程序中,例如,你可以使用一个XPath表达式(我认为)来计算不需要的属性标记,然后在count> 0时产生异常。它听起来不错吗? –