 |
| 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
|
|
|
|

November 30th, 2011, 05:24 AM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
decoding base64 to ascii using XSL
Hi,
I'm trying to decode some base64 data in to ascii using xsl. But i'm not having much luck with it. Could anyone pls help.
input xml
<?xml version="1.0"?>
<document version="7.0" maintenanceversion="3.0" replicaid="80257007002C4635" form="CD" parent="7E58DB90763E1CB4802576540032636D" response="true">
<item name='Body' sign='true' seal='true'>
<rawitemdata type='19'>
AgADAAAAAQCnACsAfAAAAAAAAAANCi0tLS0tLV89X05leHRQYX J0XzAwMV8wMUNBQjkzNS42NjUz
NzRCOQ0KQ29udGVudC1UcmFuc2Zlci1FbmNvZGluZzogN2JpdA 0KQ29udGVudC1UeXBlOiBtdWx0
aXBhcnQvYWx0ZXJuYXRpdmU7DQogYm91bmRhcnk9Ii0tLS1fPV 9OZXh0UGFydF8wMDJfMDFDQUI5
MzUuNjY1Mzc0QjkiDQoNCg==
</rawitemdata></item>
<item name="$FILE" summary="true" sign="true" seal="true">
<object>
<file hosttype="msdos" compression="none" flags="storedindoc" encoding="none" name="(#111585410) Document.pdf">
<created>
<datetime>20100301T055123,61-06</datetime>
</created>
<modified>
<datetime>20100301T055123,61-06</datetime>
</modified>
<filedata>
JVBERi0xLjQKCjIgMCBvYmoKPDwvVHlwZS9YT2JqZWN0L1N1Yn R5cGUvSW1hZ2UKL1dpZHRoIDEy
ODAgL0hlaWdodCAxNzQ0Ci9Db2xvclNwYWNlL0RldmljZUdyYX kKL0JpdHNQZXJDb21wb25lbnQg
OAovTGVu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:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" xmlns:str="http://exslt.org/strings" xmlns:fn="http://www.w3.org/2005/xpath-functions" version="1.0">
<xsl:output encoding="us-ascii"/>
<xsl:variable name="uid">
<xsl:value-of select="//*[local-name()='document']/*[local-name()='noteinfo']/*[local-name()='@unid']"/>
</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates select="//*[local-name()='document']"/>
</xsl:template>
<xsl:template match="*[local-name()='document']">
<html>
<body>
<xsl:apply-templates select="*[local-name()='noteinfo'] | *[local-name()='item']"/>
</body>
</html>
</xsl:template>
<xsl:template match="*[local-name()='noteinfo']">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*[local-name()='created'] | *[local-name()='modified'] | *[local-name()='revised'] | *[local-name()='lastaccessed'] | *[local-name()='addedtofile']">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*[local-name()='item']">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*[local-name()='datetime'] | *[local-name()='text'] | *[local-name()='number']">
<xsl:variable name="inputId">
<xsl:choose>
<xsl:when test="../@name!=''">
<xsl:value-of select="../@name"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="name(..)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<input>
<xsl:attribute name="style">display:none;</xsl:attribute>
<xsl:attribute name="name"><xsl:value-of select="$inputId"/></xsl:attribute>
<xsl:attribute name="id"><xsl:value-of select="$inputId"/></xsl:attribute>
<xsl:attribute name="value"><xsl:call-template name="changeLessGreat"><xsl:with-param name="value" select="."/></xsl:call-template></xsl:attribute>
</input>
</xsl:template>
<xsl:template name="changeLessGreat">
<xsl:param name="text"/>
<xsl:variable name="conversion1">
<xsl:call-template name="changeGreater">
<xsl:with-param name="value" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="changeLessThan">
<xsl:with-param name="value" select="$conversion1"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="changeGreater">
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="contains ($value, '>')">
<xsl:value-of select="substring-before ($value, '>')"/>
<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
<xsl:call-template name="changeGreater">
<xsl:with-param name="value" select="substring-after ($value, '>')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="changeLessThan">
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="contains ($value, '<')">
<xsl:value-of select="substring-before ($value, '<')"/>
<xsl:text disable-output-escaping="yes">&lt;</xsl:text>
<xsl:call-template name="changeLessThan">
<xsl:with-param name="value" select="substring-after ($value, '<')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*[local-name()='object']">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*[local-name()='file']">
<xsl:apply-templates select ="*[local-name()='filedata']"/>
</xsl:template>
<xsl:template match="*[local-name()='filedata']">
<span style="display: none;" name="$FILE">
<xsl:attribute name="id"><xsl:value-of select="../@name"/></xsl:attribute>
<xsl:apply-templates/>
</span>
</xsl:template>
<xsl:template match="*[local-name()='rawitemdata']">
<xsl:variable name="fullData">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:attribute name="id"><xsl:value-of select="concat('rawitemdata_',../@name)"/></xsl:attribute>
<xsl:choose>
<xsl:when test="string-length($fullData) >= 30">
<xsl:variable name="trimmedData">
<xsl:call-template name="rawDataConverter">
<xsl:with-param name="data"><xsl:value-of select="normalize-space(substring($fullData, 30, string-length($fullData)))" disable-output-escaping="yes"/></xsl:with-param>
</xsl:call-template></xsl:variable>
<span>
<xsl:value-of select = "$trimmedData"/>
</span>
</xsl:when>
<xsl:otherwise>
<span style="display: none;">
<xsl:apply-templates/>
</span>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- changes start here -->
<xsl:template name="rawDataConverter">
<xsl:param name="data">
</xsl:param>
<xsl:choose><xsl:when test="contains($data, '<HTML') or contains($data, '<html')">
<xsl:call-template name="htmlDataConverter">
<xsl:with-param name="trimmedData">
<xsl:value-of select="$data"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="nonHtmlDataConverter">
<xsl:with-param name="trimmedData">
<xsl:value-of select="$data"/>
</xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="htmlDataConverter">
<xsl:param name="trimmedData"></xsl:param>
<xsl:value-of select="substring-before(substring-after($trimmedData, 'BODY>'), '</BODY')"/>
</xsl:template>
<xsl:template name="nonHtmlDataConverter">
<xsl:param name="trimmedData"></xsl:param>
<xsl:value-of select="normalize-space(concat('<DIV><P>',$trimmedData,'</P></DIV>'))" disable-output-escaping="yes"/></xsl:template>
</xsl:stylesheet>
|
|

November 30th, 2011, 05:50 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Sorry, I can't tell from what you have provided what you are actually trying to output.
Base64 is Ascii (the whole point of it is to encode binary data as ascii). If you are trying to decode the base 64 then you have no guarantee that you will have ascii afterwards as well.
|
|

November 30th, 2011, 06:19 AM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
i want to get the text context of the encoded characters eg.
AgADAAAAAgDaBysAXQAAAAAAAAANCi0tLS0tLV89X05leHRQYX J0XzAwMl8wMUNBQjkzNS42NjUz
NzRCOQ0KQ29udGVudC1UcmFuc2Zlci1FbmNvZGluZzogcXVvdG VkLXByaW50YWJsZQ0KQ29udGVu
dC1UeXBlOiB0ZXh0L3BsYWluOw0KIGNoYXJzZXQ9dXMtYXNjaW kNCg0KSGkgSmVyZW15DQo9MjAN
CkkgYXdhaXQgeW91ciBmdXJ0aGVyIGFkdmljZXMgb24gdGhpcy BjbGFpbSBhcyBzb29uIGFzIHBv
c3NpYmxlLg0KPTIwDQpSZWdhcmRzDQpEYXJyZW4NCg0KX19fX1 9fX19fX19fX19fX19fX19fX19f
should get the follwing text
------_=_NextPart_002_01CAB935.665374B9
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=us-ascii
Hi Jeremy ...
|
|

November 30th, 2011, 06:43 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
I would suggest to easiest way to do this would be to write an extension function (in Java or C# or whatever environment you are in.
Alternatively you could simply google for "base 64 decode xslt" and click the first link.
|
|

November 30th, 2011, 11:17 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
In my experience Base64 is more likely to represent a UTF-8 encoding of text rather than an ASCII encoding.
Anyway, I started by converting it to hexBinary like this:
xs:hexBinary(xs:base64Binary(
translate(normalize-space(//rawitemdata), ' ', '')))
which produced this:
0200030000000100A7002B007C000000000000000D0A2D2D.. ..
which doesn't look very much like either ASCII or UTF-8 to me. If it were either of those, you could decode it using the extension function saxon:base64Binary-to-string(), but it seems it isn't, so trying to do so gives you an error saying there are invalid characters.
So, the octets represented by this base64 content don't seem to be ASCII characters, and there's not much hope of making sense of them unless you know what they are.
Actually if we look a bit further on we see this:
0200030000000100A7002B007C000000000000000D0A
2D2D2D2D2D2D5F3D5F4E657874506172745F3030315F303143 41423933352E
36363533373442390D0A
436F6E74656E742D5472616E736665722D456E636F64696E67 3A20376269740D0A
436F6E74656E742D547970653A206D756C7469706172742F61 6C7465726E61
746976653B0D0A
20626F756E646172793D222D2D2D2D5F3D5F4E657874506172 745F3030325
F30314341423933352E3636353337344239220D0A
0D0A
So perhaps there is one line of binary followed by a number of lines of ASCII; if you can manage to strip off the binary then the Saxon extension function might be able to deal with the rest.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

December 2nd, 2011, 11:45 AM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Thank you all for your assistance. I have mangeed to the decoding. There's one more issue i'm having.
The output i get looks like this
<span><DIV><P>------_=_NextPart_002_01CAB935.665374B9 Content-Transfer-Encoding: Content-T<span><DIV>....
I would like to replace <DIV> with <DIV>. Is there a way to replace all the <DIV>'s in the document
thank you
|
|

December 2nd, 2011, 12:17 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Your output doesn't look well-formed and I don't know how you are generating it. I'll need to know that if I'm going to advise you how to generate different output.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

December 2nd, 2011, 12:42 PM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
The following input is generated by datapower.
<?xml version="1.0" encoding="UTF-8"?>
<testnode>
<span><DIV><P>This e-mail message is confidential and for use by the addressee only. If the message is received by anyone other than the addressee, please return the message to the sender by replying to it and then delete the message from your computer. Internet e-mails are not necessarily secure. </P></DIV></span>
</testnode>
I want to replace <DIV> to <DIV> like wise </DIV>, <P>, </P> using xslt
|
|

December 2nd, 2011, 01:02 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
XSLT works on trees, not on lexical tags.
But you can do this with rules of the form
Code:
<xsl:template match="DIV">
<xsl:text><DIV></xsl:text>
<xsl:apply-templates/>
</xsl:text></DIV></xsl:text>
</xsl:template>
You may not get exactly the output you wanted, for example you may get & gt; rather than >, but it will be equivalent at the XML level.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|
 |