Remove child node results in empty parent node

2019-09-06 16:29发布

I am new to XSLT and I followed the article below to implement my requirement. My requirement is to remove the parent node completely if a condition in the child node is met. Same as the article below.

deleting the parent node if child node is not present in xml using xslt

In reference to the example in the above article, I was able to remove the content under <car>. However, my source xml has a twist. The twist is that it has a namespace.

For example:

source xml

<car xmlns="urn:test:v2xml">
 .....
</car>

output xml:

<car/>

In my implementation, the result was an empty car node in my XML output. I am expecting the car node to be completed removed but no matter what I tried, the empty car node remains in the output xml.

Is there a way to completely remove that car node? I suspect that my namespace has something to do with it because without the namespace, it works and the tag is completely removed.

标签: xml xslt
2条回答
可以哭但决不认输i
2楼-- · 2019-09-06 17:02

I was able to achieve what I wanted by chaining a second transformation process to remove the empty node. It's called multi-pass. Thus, in a single XSLT stylesheet, the first output was created with the empty node. Then in a second transformation in the same stylesheet, a search is made to remove the empty nodes. The final output provided the cleaned result. Example of two-pass transformation is here:

Two phase processing: Do not output empty tags from phase-1 XSLT 2.0 processing

查看更多
甜甜的少女心
3楼-- · 2019-09-06 17:04

You'll need to specify the namespace for car, taking care of whether you want to squelch car or Car:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:v2="urn:test:v2xml"
                version="1.0">
    <!--Identity template to copy all content by default-->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="v2:car[not(Price)]"/>
</xsl:stylesheet>
查看更多
登录 后发表回答