xsl:value-of select and xsl:template match issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • genox
    New Member
    • Jul 2010
    • 2

    xsl:value-of select and xsl:template match issue

    Hi All,

    A newbie to the forum and the world of XML/XSLT

    I'm trying to create a XSLT script to convert from one XML format to another (cXML is the target format) for a system integration project.

    I want to be able to map values in the source XML tags to the destination cXML tags as necessary but am stumbling on problems retrieving the right elements

    Here comes the code...

    Source XML to be converted

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <PublishMXPO_GOSOP xmlns="http://www.ibm.com/maximo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creationDateTime="2010-07-07T10:22:53+01:00" transLanguage="EN" baseLanguage="EN" messageID="1278494573441131060" maximoVersion="7 1 20090627-0754 V7115-149" event="0">
      <MXPO_GOSOPSet>
        <PO>
          <BILLTO>BAKEWELL</BILLTO>
          <BILLTOATTN>PMPRBOWNUSR</BILLTOATTN>
        </PO>
      </MXPO_GOSOPSet>
    </PublishMXPO_GOSOP>
    XSL script

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" xmlns:xalan="http://xml.apache.org/xslt">
    
    	<xsl:output method="xml" version="1.0" doctype-system="http://xml.cxml.org/schemas/cXML/1.2.020/cXML.dtd" indent="yes"/>
    	<xsl:variable name="version">1.2.020</xsl:variable>
    
    	<xsl:template match="/PublishMXPO_GOSOP">
    			<xsl:apply-templates select="PublishMXPO_GOSOP"/>
    	</xsl:template>
    	
    	<xsl:template match="*"> 
    		<xsl:variable name="messageID" select="@messageID"/>  
    		<xsl:variable name="timestamp" select="@creationDateTime"/> 
    		<xsl:variable name="version" select="$version"/> 
    	
    				<cXML payloadID="{$messageID}" timestamp="{$timestamp}" version="{$version}" > 
    					<BILLADDRESS><xsl:value-of select="*"/></BILLADDRESS>
    					<BILLTOPERSON><xsl:value-of select="PublishMXPO_GOSOP/MXPO_GOSOPSet/PO/BILLTOATTN"/></BILLTOPERSON>
    				</cXML> 		 
    	</xsl:template>	
    </xsl:stylesheet>
    Current output XML

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.020/cXML.dtd">
    <cXML xmlns:xalan="http://xml.apache.org/xslt" version="1.2.020" timestamp="2010-07-07T10:22:53+01:00" payloadID="1278494573441131060">
    <BILLADDRESS>
        
          BAKEWELL
          PMPRBOWNUSR
        
      </BILLADDRESS>
    <BILLTOPERSON/>
    </cXML>
    I want to be able to split the two (and more) element values out into their respective target tags. Any clues on how to do this?

    I've only managed to get anything returned from the source XML file by using xsl:value-of select="*". Using an XPATH expression like in <BILLTOATTN> does not work

    Also, when I change the first template-match statement to / from /PublishMXPO_GOS OP I lose everything in the conversion except the xml version encoding header? I thought this should select all the nodes in the whole document?

    Any advice on this is really appreciated

    Cheers

    Eugene
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Also, when I change the first template-match statement to / from /PublishMXPO_GOS OP I lose everything in the conversion except the xml version encoding header? I thought this should select all the nodes in the whole document?
    / stands for the root document, // lets you select from everywhere. if you want to select all nodes, use *

    what does the target XML should look like?
    Last edited by Dormilich; Jul 9 '10, 09:47 AM.

    Comment

    • genox
      New Member
      • Jul 2010
      • 2

      #3
      Hi

      Thanks for the quick response! :-)

      Target XML should look like this:

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.020/cXML.dtd">
      <cXML xmlns:xalan="http://xml.apache.org/xslt" version="1.2.020" timestamp="2010-07-07T10:22:53+01:00" payloadID="1278494573441131060">
      	<BILLADDRESS>BAKEWELL</BILLADDRESS>
      	<BILLTOPERSON>PMPRBOWNUSR<BILLTOPERSON/>
      </cXML>
      Cheers

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I think you need to use the <xsl:copy> element, but I’m not an expert in copying.

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          You're missing namespaces. Put xmlns:maximo="h ttp://www.ibm.com/maximo" in your stylesheet node. So it should be instead:
          <xsl:template match="maximo:P ublishMXPO_GOSO P">

          Also, you don't have to use variables; you can put the xpaths directly in the braces{} (unless you're using the variables for some sort of debugging purpose).

          Comment

          Working...