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

前端 未结 3 1408
醉话见心
醉话见心 2020-12-19 19:00

I am trying to transform a given XML using xslt. The caveat is that I would have to delete a parent node if a given child node is not present. I did do some template matchin

相关标签:
3条回答
  • 2020-12-19 19:26

    A different solution is to use the 'xsl:copy-of' element.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
    
        <xsl:output method="xml" />
    
        <xsl:template match="Cars">
            <xsl:copy>
                <xsl:copy-of select="Car[Price]" />
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    
    0 讨论(0)
  • 2020-12-19 19:33

    Super close. Just change your last template to:

    <xsl:template match="Car[not(Price)]"/>
    

    Also, it's not incorrect but you can combine your 2 xsl:output elements:

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    
    0 讨论(0)
  • 2020-12-19 19:37
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        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="Car[not(Price)]"/>
    
    </xsl:stylesheet>
    
    0 讨论(0)
提交回复
热议问题