0

I am receiving XML from a third party that conforms to NIEM standards. I need to translate it into a flat text file for another application to use. I've done this before using XSLT in another project, but not with quite so many namespaces. The XML document I'm receiving contains 5 namespaces and each element is individually prefixed with the required namespace. I'm having a hard time figuring out how to access the data with all the different namespaces. Any help to get me started would be appreciated.

Partial XML File Sample

    <?xml version="1.0" encoding="UTF-8"?>
    <asap:ReportTransmission xmlns:asap="http://www.asapnet.org/pmp/4.2/exchange"
     xmlns:asap-code="http://www.asapnet.org/pmp/4.2/extension/code"
     xmlns:asap-ext="http://www.asapnet.org/pmp/4.2/extension"
     xmlns:asap-meta="http://www.asapnet.org/pmp/4.2/extension/meta"
     xmlns:nc="http://release.niem.gov/niem/niem-core/3.0/" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.asapnet.org/pmp/4.2/exchange ../schemas/exchange/pmp_exchange.xsd">
        <asap-meta:TransactionHeader>
            <asap-meta:ReleaseNumberText>4.2</asap-meta:ReleaseNumberText>
            <asap-meta:ControlNumberText>857463</asap-meta:ControlNumberText>
            <asap-code:TransactionKindCode>01</asap-code:TransactionKindCode>
            <asap-meta:TransactionDate>2009-10-15</asap-meta:TransactionDate>
            <asap-meta:TransactionTime>10:45:00</asap-meta:TransactionTime>
            <asap-code:FileKindCode>P</asap-code:FileKindCode>
        </asap-meta:TransactionHeader>

   ... continues
</asap:ReportTransmission>

I have set up my stylesheet based on this example on MSDN ("XSLT and Namespaces" section): https://msdn.microsoft.com/en-us/library/ms950779.aspx?f=255&MSPPError=-2147217396

The accepted answer to this question is very similar to the MSDN example and seems to confirm how it should be done: XSLT with multiple namespaces

Current Stylesheet

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

    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                    xmlns:asap="http://www.asapnet.org/pmp/4.2/exchange"
                    xmlns:asap-code="http://www.asapnet.org/pmp/4.2/extension/code"
                    xmlns:asap-ext="http://www.asapnet.org/pmp/4.2/extension"
                    xmlns:asap-meta="http://www.asapnet.org/pmp/4.2/extension/meta"
                    xmlns:nc="http://release.niem.gov/niem/niem-core/3.0/"
                    exclude-result-prefixes="asap asap-code asap-ext asap-meta nc">

      <xsl:output method="text" omit-xml-declaration="yes" indent="no" />

      <xsl:template match="asap:ReportTransmission">
    <xsl:apply-templates select="asap-meta:TransactionHeader"/>
  </xsl:template>

  <xsl:template match="asap-meta:TransactionHeader">
        <xsl:value-of select="asap-meta:ReleaseNumberText"/>
        <xsl:value-of select="ReleaseNumberText"/>
  </xsl:template>
</xsl:stylesheet>

I'm not getting a transformed result at all, the source XML is echoed back as the result.

What is the proper way to match and select data with so many namespaces?

8
  • Your transform as you have it with the given XML selects fine. (however <xsl:value-of select="ReleaseNumberText"/> yields nothing as expected) Please provide a more representative example. Commented Oct 9, 2015 at 20:03
  • The second line was me just trying anything to select the value. Neither of those lines select a value. I expected the first one to return the value but it doesn't. If I try to debug the stylesheet and run the 1st line in the immediate window it results in error message "Expression must evaluate to a node-set". I'm not sure what you mean by a more representative example. This is representative of what I'm trying to accomplish. I need to select the data and shove it into a text file. I'm not worried about formatting at the moment, just getting something to actually select. Commented Oct 9, 2015 at 20:13
  • 2
    I'm saying your example isn't failing like you're claiming. It should and does get a result. The error you mention is an error that would not happen with your example, you've omitted the parts that would yield that error. You need to include those parts into your question. Commented Oct 9, 2015 at 20:22
  • I removed the second line. At this point, that's all I have in my stylesheet. And it yields no results at all. Commented Oct 9, 2015 at 20:25
  • 1
    @mslissap As Jeff Mercado says, the problem cannot be reproduced using your code: xsltransform.net/jyRYYhV Commented Oct 9, 2015 at 20:42

0

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.