Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 16th, 2012, 03:49 PM
Registered User
 
Join Date: Mar 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default xml copy with text substitution from map P2: problem with text outside CDATA

Hi,
I previously posted under [xml copy with text substitution from map] and with
the help of folks here i got a working transform. However, I have discovered
one error. I am using a map to translates UIDs to text values, it works for
elements found in CDATA that have been decoded using saxon.parse() but
does not work for elements that were outside the CDATA.

I hope you can set me figure this out, it has been driving my crazy for 2 days.

Thanks in advance,
Tim

In the example below see: sEventDefinitionID (ouside CDATA) and
ENTITYTYPEID (inside CDATA) both have the same UID value.

From the Console output below I have verified the following:
The UID [b02fb601b98a47ce8de32dbb157fb2a2] is in the map.
It doesn't have extra characters.
It is going thru the mapping template.
After passing thru the saxon.parse() the UID is correctly transformed to
POWER_FAILURE.

Console output:
Code:
PRE-MAP:raw-value: [b02fb601b98a47ce8de32dbb157fb2a2]
POST-MAP:[b02fb601b98a47ce8de32dbb157fb2a2]
PRE-MAP:raw-value: [b02fb601b98a47ce8de32dbb157fb2a2]
POST-MAP:[POWER_FAILURE]
Translated Ouput:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ReceivePanoramixEvents xmlns="http://eventreceiver.example.com/"
                              soap:EncodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <sEventDefinitionID xsi:type="xsd:string">b02fb601b98a47ce8de32dbb157fb2a2</sEventDefinitionID>
         <sXmlParameters>
            <EVENT xmlns="">
               <ENTITYTYPEID>POWER_FAILURE</ENTITYTYPEID>
            </EVENT>
         </sXmlParameters>
      </ReceivePanoramixEvents>
   </soap:Body>
</soap:Envelope>
Input Soap Msg (formatted for readability)
Code:
<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'
>
  <soap:Body>
    <ReceivePanoramixEvents xmlns='http://eventreceiver.example.com/' soap:EncodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
      <sEventDefinitionID xsi:type='xsd:string'>b02fb601b98a47ce8de32dbb157fb2a2</sEventDefinitionID>
      <sXmlParameters xsi:type='xsd:string'>&lt;EVENT&gt;&lt;ENTITYTYPEID&gt;b02fb601b98a47ce8de32dbb157fb2a2&lt;/ENTITYTYPEID&gt;&lt;/EVENT&gt;
      </sXmlParameters>
    </ReceivePanoramixEvents>
  </soap:Body>
</soap:Envelope>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:saxon="http://saxon.sf.net/" xmlns:guid="java:com.me.pipe.dca.xyz.ConstantsMap" exclude-result-prefixes="saxon guid"
>
  <!--  
       This transform is used to assist in debugging SOAP msgs.
       These msgs contain CDATA, which I want to "un-escape" and
       32 Char UID values which I need to translate to their text
       equivalent. This is done for readability during debug.
   -->
   
   <!-- 
      A Map (constantsXsltMap.xml) is used to translate between UIDs
      and text values
    <map>
      <entry key="53bfdc0c8b9341688a520134b1098b26">VALUE_1</entry>
      <entry key="73bfdc0c8b9341688a520134b1098b78">VALUE_2</entry>
      <default>SOME_DEFAULT_VALUE</default>
    </map>
    -->
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
  <xsl:param name="map-file" select="string('constantsXsltMap.xml')" />
  <xsl:variable name="map-doc" select="document($map-file)" />
  <xsl:variable name="default" select="$map-doc/map/default[1]" />


  <xsl:key name="map" match="/map/entry" use="@key" />

  <!--  default copy transform -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>


  <!-- Identify element containing CDATA and perform and decode it -->
  <xsl:template match="node()[ends-with(local-name(), 'sXmlParameters')]">
    <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
      <xsl:apply-templates select="saxon:parse(.)" />
    </xsl:element>
  </xsl:template>

  <!-- Identify element containing CDATA and perform and decode it -->
  <xsl:template match="node()[ends-with(local-name(), 'Result')]">
    <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
      <xsl:apply-templates select="saxon:parse(.)" />
    </xsl:element>
  </xsl:template>

  <!-- 
     Use contents of text element as Key value in map. If key is
     found substitute with value, else leave as is 
  -->
  <xsl:template match="*/text()">
    <xsl:variable name="raw-value" select="." />
    <xsl:message>PRE-MAP:raw-value: [<xsl:copy-of select="$raw-value"/>]</xsl:message>
    <xsl:for-each select="$map-doc">
      <xsl:value-of select="(key('map', $raw-value)|$raw-value)[1]" />
      <xsl:message>POST-MAP:[<xsl:value-of select="(key('map', string($raw-value))|$raw-value)[1]"/>]</xsl:message>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>





Similar Threads
Thread Thread Starter Forum Replies Last Post
xml copy with text substitution from map mancocapac XSLT 10 April 7th, 2011 07:26 PM
Format cdata text into xml Alisha XSLT 1 January 3rd, 2011 07:23 AM
Copy text to win Clipboard irresistible007 Beginning VB 6 6 October 5th, 2006 06:04 AM
How to copy text MICZ Pro VB 6 5 October 29th, 2004 09:57 AM





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