Home > Software engineering >  Missed values while transforming XML into HTMl using XSL
Missed values while transforming XML into HTMl using XSL

Time:09-21

Some of the values of my XML file are not displaying when transformed. Looks like something is not right witn XSL namespaces, but i can't figure it out.

XML file to transform

<?xml-stylesheet type="text/xsl" href="birth_new_3.xsl"?>
<ns2:ROGDINFResponse xmlns="urn://x-artefacts-zags-rogdinf/types/4.0.1"
                     xmlns:ns2="urn://x-artefacts-zags-rogdinf/root/112-51/4.0.1">
    <ns2:ZgsAnswer>
        <ns2:BrthReg>
            <ns2:BrthLcs>
                <SerLcs>II-LP</SerLcs>
                <NumLcs>443334</NumLcs>
                <DateLcs>1988-03-18</DateLcs>
            </ns2:BrthLcs>
            <ns2:SvReg>
                <ns2:SvRodiv>
                    <ns2:FIOrogd>
                        <Last>Last</Last>
                        <First>Fname</First>
                        <Middle>Mname</Middle>
                    </ns2:FIOrogd>
                    <ns2:BirthDate>1988-01-19</ns2:BirthDate>
                    <ns2:BirthPlace Place="Some place"/>
                </ns2:SvRodiv>
            </ns2:SvReg>
        </ns2:BrthReg>
    </ns2:ZgsAnswer>
</ns2:ROGDINFResponse>

XSL file

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns2="urn://x-artefacts-zags-rogdinf/root/112-51/4.0.1"
                xmlns="urn://x-artefacts-zags-rogdinf/types/4.0.1">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <HTML>
            <BODY>
                <H2>Birth data</H2>
                <span>Serial: <b>
                        <xsl:apply-templates select="//ns2:ROGDINFResponse/ns2:ZgsAnswer/ns2:BrthReg/ns2:BrthLcs/SerLcs"/>
                    </b>
                </span>
                <p>             
                <span>Number: <b>
                        <xsl:apply-templates select="//ns2:ROGDINFResponse/ns2:ZgsAnswer/ns2:BrthReg/ns2:BrthLcs/NumLcs"/>
                    </b>
                </span>
                </p>
                <p>
                <span>Date: <b>
                        <xsl:apply-templates select="//ns2:ROGDINFResponse/ns2:ZgsAnswer/ns2:BrthReg/ns2:BrthLcs/DateLcs"/>
                    </b>
                </span> 
                </p>
                <H3>Additional data: </H3>
                <span>Last, First, Middle:
                            <b>
                                <xsl:apply-templates select="//ns2:ROGDINFResponse/ns2:ZgsAnswer/ns2:BrthReg/ns2:SvReg/ns2:SvRodiv/ns2:FIOrogd/Last"/>
                            </b>
                            <b>
                                <xsl:apply-templates select="//ns2:ROGDINFResponse/ns2:ZgsAnswer/ns2:BrthReg/ns2:SvReg/ns2:SvRodiv/ns2:FIOrogd/First"/>
                            </b>
                            <b>
                                <xsl:apply-templates select="//ns2:ROGDINFResponse/ns2:ZgsAnswer/ns2:BrthReg/ns2:SvReg/ns2:SvRodiv/ns2:FIOrogd/Middle"/>
                            </b>
                </span>
                <p>
                <span>Date: <b>
                        <xsl:apply-templates select="//ns2:ROGDINFResponse/ns2:ZgsAnswer/ns2:BrthReg/ns2:SvReg/ns2:SvRodiv/ns2:BirthDate"/>
                    </b>
                </span>
                </p>
                <p>
                <span>Place: <b>
                        <xsl:apply-templates select="//ns2:ROGDINFResponse/ns2:ZgsAnswer/ns2:BrthReg/ns2:SvReg/ns2:SvRodiv/ns2:BirthPlace/@Place"/>
                    </b>
                </span>
                </p>
            </BODY>
        </HTML>
    </xsl:template>
</xsl:stylesheet>

Some of the values of my XML are diplaying just fine, but values of the fields without namespaces are not diplaying (SerLcs, NumLcs, DateLcs, Last, First, Middle). How can i fix my XSL?

CodePudding user response:

Instead of xmlns="urn://x-artefacts-zags-rogdinf/types/4.0.1" in the XSLT you want xpath-default-namespace="urn://x-artefacts-zags-rogdinf/types/4.0.1" in there. Note that this only works with XSLT 2 and 3 processors like Saxon 9 or 10, Saxon-JS 2, XmlPrime, Exselt, AltovaXML.

CodePudding user response:

First, Last, and Middle do have a namespace: they're in namespace urn://x-artefacts-zags-rogdinf/types/4.0.1. In XPath by default an unprefixed name refers to an element in no namespace. You can change this using xpath-default-namespace as @MartinHonnen suggests.

  • Related