XPath Queries not working correctly in Schema

Discussion in 'XML' started by rosemm, Apr 5, 2005.

  1. rosemm

    rosemm Guest

    i have a file which I get from a vendor. we are using a industry
    schema, hr-xml. however, I can't seem to get any XPath expresions to
    work when i receive the file. i think i have pinpointed the problem but
    now I need a solution.

    I have the following template

    <xsl:template match="//PersonName">
    <xsl:value-of select="normalize-space(FamilyName)"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="normalize-space(GivenName)"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="normalize-space(MiddleName)"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="normalize-space(Affix)"/>
    <xsl:text>,</xsl:text>
    </xsl:template>

    The beginning of my xml file looks like this (this is where the problem
    is)

    <Enrollment xmlns="http://ns.hr-xml.org/2004-08-02"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://ns.hr-xml.org/2004-08-02
    Enrollment.xsd" creationDate="2005-03-31T11:47:26-06:00">

    With this I get no data out of my file.

    When I change that to

    <Enrollment xmlns="creationDate="2005-03-31T11:47:26-06:00">

    the above template works and I retrieve the names of everyone in the
    file.

    Anyone know exactly what the problem is and how I can fix it?
    rosemm, Apr 5, 2005
    #1
    1. Advertising

  2. This is the most F of faqs,

    <xsl:template match="//PersonName">

    which matches the same things as

    <xsl:template match="PersonName">

    a leading // never does anything useful in a match pattern, it only
    changes the default priority.

    matches PersonName in no-namespace.

    similarly

    <xsl:value-of select="normalize-space(FamilyName)"/>

    selects FamilyName in no-namespace

    <Enrollment xmlns="http://ns.hr-xml.org/2004-08-02"

    sets the default namespace to http://ns.hr-xml.org/2004-08-02
    and I would guess that your PersonName and
    FamilyName elements are in that namespace, so these are not matched by
    the Xpath expressions above.

    add

    xmlns:h="http://ns.hr-xml.org/2004-08-02"

    to your xsl:stylesheet and then use

    <xsl:template match="h:personName">
    <xsl:value-of select="normalize-space(h:FamilyName)"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="normalize-space(h:GivenName)"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="normalize-space(h:MiddleName)"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="normalize-space(h:Affix)"/>
    <xsl:text>,</xsl:text>
    </xsl:template>


    David
    David Carlisle, Apr 5, 2005
    #2
    1. Advertising

  3. rosemm wrote:

    > i have a file which I get from a vendor. we are using a industry
    > schema, hr-xml. however, I can't seem to get any XPath expresions to
    > work when i receive the file. i think i have pinpointed the problem but
    > now I need a solution.
    >
    > I have the following template
    >
    > <xsl:template match="//PersonName">
    > <xsl:value-of select="normalize-space(FamilyName)"/>
    > <xsl:text>,</xsl:text>
    > <xsl:value-of select="normalize-space(GivenName)"/>
    > <xsl:text>,</xsl:text>
    > <xsl:value-of select="normalize-space(MiddleName)"/>
    > <xsl:text>,</xsl:text>
    > <xsl:value-of select="normalize-space(Affix)"/>
    > <xsl:text>,</xsl:text>
    > </xsl:template>
    >
    > The beginning of my xml file looks like this (this is where the problem
    > is)
    >
    > <Enrollment xmlns="http://ns.hr-xml.org/2004-08-02"
    > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xsi:schemaLocation="http://ns.hr-xml.org/2004-08-02
    > Enrollment.xsd" creationDate="2005-03-31T11:47:26-06:00">
    >
    > With this I get no data out of my file.


    The elements are probably in the default namespace so to select them
    with an XPath 1.0 expression you need to bind a prefix to that namespace
    in the stylesheet e.g.
    <xsl:stylesheet
    xmlns:hr="http://ns.hr-xml.org/2004-08-02"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    then use that prefix in your XPath expression e.g.
    //hr:person
    or
    <xsl:value-of select="normalize-space(hr:FamilyName)"/>

    See also
    <http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/1753>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Apr 5, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thedebugger
    Replies:
    0
    Views:
    339
    thedebugger
    Nov 4, 2004
  2. Markus
    Replies:
    1
    Views:
    1,497
    Markus
    Nov 23, 2005
  3. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,170
    Stanimir Stamenkov
    Apr 25, 2005
  4. Ian Roddis

    xslt queries in xml to SQL queries

    Ian Roddis, Feb 26, 2006, in forum: Python
    Replies:
    3
    Views:
    1,470
    Crutcher
    Feb 26, 2006
  5. Abby Lee

    so many queries within queries I'm confused

    Abby Lee, Aug 4, 2004, in forum: ASP General
    Replies:
    11
    Views:
    338
    Aaron [SQL Server MVP]
    Aug 6, 2004
Loading...

Share This Page