0

I am trying to get a particular field that only occurs once inside a xml file which always has the same format, which should have been a simple task.

The field I want is "MaterialDefinitionID", I've used firefox firebug to copy the xpath which worked for all the other documents I've done except this one. At first I thought the XPATH was something complicated that firebug didn't get right but it appears to be valid to me.

The XPATH I am using is: /CPS/DA/ScghP/lkm123/ReqSqe/MATreq/MaterialDefinitionID

The xml file I am trying to get the data from:

<?xml version="1.0" encoding="UTF-8"?>

<CPS xmlns="http://www.wonderware.com/wwei/v0201/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  <AA/>  
  <DA> 
    <ScghP> 
      <ID>1741284-WOG</ID>  
      <Location/>  
      <PublishedDate>2014-03-05 16:03:36</PublishedDate>  
      <StartTime>1/28/2014 12:00:00 AM</StartTime>  
      <EndTime>1/28/2014 1:27:44 AM</EndTime>  
      <lkm123> 
        <ID>54123s0</ID>  
        <OLKM>54123s0 (EPIC Bar)</OLKM>  
        <StartTime>1/28/2014 12:00:00 AM</StartTime>  
        <EndTime>1/28/2014 1:27:44 AM</EndTime>  
        <Priority>0</Priority>  
        <ReqSqe> 
          <ID>000xxxx</ID>  
          <Description>epic item</Description>  
          <EarliestStartTime>1/28/2014 12:00:00 AM</EarliestStartTime>  
          <LatestEndTime>1/28/2014 1:27:44 AM</LatestEndTime>  
          <Duration>0H00M00SPT0</Duration>  
          <MArked/>  
          <MATreq> 
            <MaterialDefinitionID>54123s0</MaterialDefinitionID>  
            <Description>EPIC Bar</Description>  
            <Location/>  
            <Quantity/>  
            <MPRPAZ/>  
            <MPRPAZ/>  
            <LKPK>Required</LKPK> 
          </MATreq>  
          <ReqSqe/>  
          <LKPK>Required</LKPK> 
        </ReqSqe> 
      </lkm123>  
      <Any/> 
    </ScghP> 
  </DA> 
</CPS>

The document has been refactored to protect the client's data but the structure for the relevant
components has been preserved.
Thank you for your time and any help offered.

EDIT: So I am trying to define a namespace and fetch the MaterialDefinitionID field using XSLT, the script I have so far is:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ww="wonderware.com/wwei/v0201/schemas">

<xsl:template match="/">
    <xsl:for-each select="ww:CPS/ww:DA/ww:ScghP/ww:lkm123/ww:ReqSqe/ww:MATreq">
MaterialDefinitionID:  <xsl:apply-templates select="ww:MaterialDefinitionID"/>;
    </xsl:for-each>

</xsl:template>

</xsl:stylesheet>

Something exactly like this worked for other files that didn't have the namespace issue, but not
here for some reason.

1 Answer 1

1

It's the usual namespace issue - xmlns="http://www.wonderware.com/wwei/v0201/schemas" in your XML means that all element names in that document that don't have a prefix belong to that namespace, but unprefixed names in XPath refer to elements in no namespace. You need to bind a prefix to the namespace using whatever mechanism your XPath library or tool provides, and then use that prefix in the XPath expression:

/ww:CPS/ww:DA/ww:ScghP/ww:lkm123/ww:ReqSqe/ww:MATreq/ww:MaterialDefinitionID

(where the ww prefix is bound to the http://www.wonderware.com/wwei/v0201/schemas URI)

Sign up to request clarification or add additional context in comments.

3 Comments

So if I was using XSLT I would do something like: <xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform" xmlns:ww="wonderware.com/wwei/v0201/schemas"> to bind ww and then use ww: in the XPATH later on as you describe?
or should I define a default namespace like: xpath-default-namespace="wonderware.com/wwei/v0201/schemas"
@jtempelm xpath-default-namespace is an XSLT 2.0 addition. In XPath 1.0 (and hence in XSLT 1.0) unprefixed names are fixed in the XPath spec to always mean no namespace, so the only way to match namespaced nodes is to use prefixes (or to use tricks like *[local-name() = 'foo' and namespace-uri() = 'http://example.com/bar'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.