在xml层次结构JAVA中查找特定节点

时间:2016-11-23 22:53:31

标签: java xml xslt nodes sax

我正在使用SAX转换XML文档并使用xsl:stylesheet删除节点(感谢teppic)。我不熟悉XML以了解如何编辑文档。

xsl:

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

<!-- Strip IMFile elements -->
<xsl:template match="IMFile"/>

这将获取IMFile的所有节点并完美删除它们。我现在需要搜索类型的节点:Callout并查看他们的VectorNode值的子节点是否等于TypeWinText,如果是,则删除整个Callout节点。如果不是 - 什么也不做。

Project_Data Version="8.00"> 
<CSMLData> 
<GoProject id="1" version="3.0" > <Project id="2" editRate="30/1" version="3.0" > 
<Timeline id="6" > 
<GenericMixer id="10" name="Unified Mixer"> 
<Tracks> 
<GenericTrack id="11" > 
<Medias> 
<Callout id="91" start="55" duration="20" scalar="1/1" mediaStart="25/1" mediaDuration="20/1" > 
<Attributes> 
<Attribute id="130" name="vectorNode"> 
<VectorNode id="131" kind="TypeWinSVG" > </VectorNode>

2 个答案:

答案 0 :(得分:0)

Callout节点在什么位置,你能更具体一点吗?

要搜索节点是否具有属性x,您可以执行以下操作:

<xsl:template match="Keyframes">
 <xsl:choose>
         <xsl:when test="Keyframe/@value='x'">
              //do what you want for example
           <xsl:value-of select="Keyframe/@value>
              // this will print "x"
         </xsl:when>
 </xsl:choose>
</xsl:template>

您可以通过

应用模板
<xsl:apply-templates select="Keyframes"/>

答案 1 :(得分:0)

考虑添加另一个空模板,根据具体情况删除标注

<xsl:template match="Callout[descendant::VectorNode/@kind='TypeWinText']"/>