p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   template match doesnt match the required node (http://p2p.wrox.com/showthread.php?t=55247)

Tomi March 12th, 2007 05:49 AM

template match doesnt match the required node
 
Hello all,

I'm having problems when matching the nodes of an xml file.I think there is a problem with namespaces but I cant figure out which one.Ive already read the link http://www.dpawson.co.uk/xsl/sect2/N...tml#d7563e1012 but it didnt help me in my specific problem.

XSL version 2.0

Processor:Saxon 8

XML input:

<?xml version="1.0" encoding="UTF-8"?>
<d2LogicalModel xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://datex2.eu/schema/1_0/1_0" modelBaseVersion="1.0" xsi:schemaLocation="http://datex2.eu/schema/1_0/1_0 C:\DOCUME~1\totres\Escritorio\d1d2transf\DATEXI~1\ DATEXIISchema_1_0_1_0\DATEXIISchema_1_0_1_0.xsd">
    <exchange>
        <supplierIdentification>
            <country>es</country>
            <nationalIdentifier>es001</nationalIdentifier>
        </supplierIdentification>
    </exchange>
    <payloadPublication xsi:type="SituationPublication" lang="es">
        <publicationTime>2007-02-21T09:32:00+01:00</publicationTime>
        <publicationCreator>
            <country>es</country>
            <nationalIdentifier>es001</nationalIdentifier>
        </publicationCreator>
        <situation id="GUID254746">
            <headerInformation>
                <confidentiality>noRestriction</confidentiality>
                <informationStatus>real</informationStatus>
            </headerInformation>
            <situationRecord xsi:type="Activities" id="GUID254746_3O">
                <situationRecordCreationTime>2006-04-04T12:16:00+01:00</situationRecordCreationTime>
                <situationRecordVersion>4</situationRecordVersion>
                <situationRecordVersionTime>2006-04-04T12:16:00+01:00</situationRecordVersionTime>
                <situationRecordFirstSupplierVersionTime>2006-04-04T12:16:00+01:00</situationRecordFirstSupplierVersionTime>
                <probabilityOfOccurrence>certain</probabilityOfOccurrence>
                <validity>
                    <validityStatus>active</validityStatus>
                    <validityTimeSpecification>
                        <overallStartTime>2006-10-17T14:00:00+02:00</overallStartTime>
                        <overallEndTime>2006-10-17T16:00:00+02:00</overallEndTime>
                    </validityTimeSpecification>
                </validity>
                <groupOfLocations>
                    <locationContainedInGroup xsi:type="Linear">
                        <alertCLinear xsi:type="AlertCMethod4Linear">
                            <alertCLocationCountryCode>E</alertCLocationCountryCode>
                            <alertCLocationTableNumber>17</alertCLocationTableNumber>
                            <alertCLocationTableVersion>2.0</alertCLocationTableVersion>
                            <alertCDirection>
                                <alertCDirectionCoded>both</alertCDirectionCoded>
                            </alertCDirection>
                            <alertCMethod4PrimaryPointLocation>
                                <alertCLocation>
                                    <specificLocation>61602</specificLocation>
                                </alertCLocation>
                                <offsetDistance>
                                    <offsetDistance>2099</offsetDistance>
                                </offsetDistance>
                            </alertCMethod4PrimaryPointLocation>
                            <alertCMethod4SecondaryPointLocation>
                                <alertCLocation>
                                    <specificLocation>61608</specificLocation>
                                </alertCLocation>
                                <offsetDistance>
                                    <offsetDistance>2099</offsetDistance>
                                </offsetDistance>
                            </alertCMethod4SecondaryPointLocation>
                        </alertCLinear>
                    </locationContainedInGroup>
                </groupOfLocations>
                <authorityOperationType>policeCheckPoint</authorityOperationType>
            </situationRecord>
        </situation>
    </payloadPublication>
</d2LogicalModel>


XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
    <xsl:param name="filtro"/>
    <!--
    Template Principal
    -->
    <xsl:template match="/">
        <xsl:apply-templates select="d2LogicalModel"/>

    </xsl:template>
    <!--
    Template
    -->
    <xsl:template match="d2LogicalModel">

        <xsl:copy>
            <xsl:copy-of select="@*"/>

            <xsl:apply-templates select="exchange"/>
            <xsl:apply-templates select="payloadPublication"/>
        </xsl:copy>
    </xsl:template>
    <!--
    Template
    -->
    <xsl:template match="exchange">
        <xsl:copy>

            <xsl:copy-of select="*"/>
        </xsl:copy>
    </xsl:template>
    <!--
    Template
    -->
    <xsl:template match="payloadPublication">
        <xsl:copy>
            <xsl:copy-of select="child::publicationTime"/>
            <xsl:copy-of select="child::publicationCreator"/>
            <xsl:apply-templates select="situation"/>
        </xsl:copy>
    </xsl:template>
    <!--
    Template
    -->
    <xsl:template match="situation">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:copy-of select="child::headerInformation"/>
            <xsl:if test="count(child::situationRecord[@xsi:type = $filtro]) != 0 ">
                <xsl:apply-templates select="situationRecord"/>
            </xsl:if>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="situationRecord">
        <xsl:if test=" @xsi:type = $filtro ">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:copy-of select="*"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>


Ive been debugging it with xml spy and I found that only the first template is executed, none of the nodes of the xml are matched except the root node('/').

Any ideas?

I will appreciate the help.

Thanks.

Tomi.




joefawcett March 12th, 2007 06:07 AM

I fail to see how the link you gave didn't help. It clearly shows that you need to declare the http://datex2.eu/schema/1_0/1_0 namespace in your stylesheet so add:
Code:

xmlns:datex="http://datex2.eu/schema/1_0/1_0"
to the xsl:stylesheet element and change your XPath, e.g.
Code:

<xsl:apply-templates select="datex:d2LogicalModel"/>
and
Code:

<xsl:template match="datex:d2LogicalModel">
I chose datex as a prefix but you can pick whatever you want.

--

Joe (Microsoft MVP - XML)

mhkay March 12th, 2007 06:24 AM

When you write

<xsl:template match="d2LogicalModel">

you are matching nodes whose name is (local-part="d2LogicalModel, namespace=null). But the node in your source document has (local-part="d2LogicalModel", namespace="http://datex2.eu/schema/1_0/1_0"). To match this node you need to write

<xsl:template match="s:d2LogicalModel">

and add xmlns:s="http://datex2.eu/schema/1_0/1_0" to your xsl:stylesheet. Similarly all the other unqualified element names in match and select attributes need to be prefixed in the same way.

Alternatively since you are using XSLT 2.0 you can add

xpath-default-namespace="http://datex2.eu/schema/1_0/1_0"

to the xsl:stylesheet element.

I'm not sure why the link you reference didn't help you since it describes exactly this problem, and its solution.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference


All times are GMT -4. The time now is 05:16 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.