Dymond
Dymond

Reputation: 2277

output text when element is missing

I bet this is a simple question.

Im trying to style my xml file with XSL. And the thing I am trying to do is that when an element in my XML file is missing, I want the xsl to output " Missin Element"

I manage to create an string that says no data in element, but thats when I don't have any data in the element but the element still exist.

I pasting my xml file to explain better

<autoads>
    <ad>
        <type>1</type>
        <name>Honda</name>
        <model>XL 1000 V</model>
        <regyear>2001</regyear>
        <price>129900</price>
        <adtext>2001 Honda XL 1000 V, 8.900 km. hög vindruta. Pris 129.900kr,-.  </adtext>
        <addate>20020115</addate>
        <volume>1000</volume>

    </ad>
    <ad>
        <type>2</type>
        <name>Nissan</name>
        <model>Almera 1.4S</model>
        <regyear>1997</regyear>
        <price>119000</price>
        <adtext>1997 Nissan Almera 1.4S, 5 dörrar, met, 70.000 km. el.spegel/fönster, galv. kaross, c.lås, startspärr, airbag, nedfällb. baks. ABS, ute temp. R/CD, alarm, d.fäste, v.säten, s/v-hj.  EU-godk. full service, servo. Pris 119.000 kr,-.  </adtext>
        <addate>20020118</addate>
        <volume>0</volume>
        <category>5 dörrar</category>
    </ad>
</autoads>

As you se the element category is missing in the first one,

Thats what im trying to output with an xsl string, that it should print out " category is missing"

Thanks for all help.

Upvotes: 1

Views: 166

Answers (1)

Wayne
Wayne

Reputation: 60414

You'll need to specify which elements you're expecting and test for their existence. In the simplest case, use something like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="ad">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:if test="not(category)">
                <xsl:comment>category is missing</xsl:comment>
            </xsl:if>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

This outputs a comment in place of the missing node, but you could easily modify it to output an element or simply text.

For a more complete solution, create a list of the required elements and use the document function to verify the presence of each item in this list:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:m="m">
    <xsl:output method="xml" indent="yes"/>
    <m:req>type</m:req>
    <m:req>name</m:req>
    <m:req>model</m:req>
    <m:req>regyear</m:req>
    <m:req>price</m:req>
    <m:req>adtext</m:req>
    <m:req>addate</m:req>
    <m:req>volume</m:req>
    <m:req>category</m:req>
    <xsl:variable name="required" select="document('')/*/m:req"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="ad">
        <xsl:variable name="this" select="."/>
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each select="$required">
                <xsl:variable name="search"
                    select="$this/*[name()=current()]"/>
                <xsl:if test="$search">
                    <xsl:apply-templates select="$search"/>
                </xsl:if>
                <xsl:if test="not($search)">
                    <xsl:comment>
                        <xsl:value-of select="concat(., ' is missing')"/>
                    </xsl:comment>
                </xsl:if>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions