Pavan Puligandla Pavan Puligandla - 3 months ago 15
C# Question

Strip encoded values between XML elements and namespaces in XSLT 1.0

I have an XML with spaces between XML elements. I am transforming it to XSL FO with XSLT 1.0 template to print a pdf report. I would like to decode the values in XSLT to print in the PDF.Below is my XML and XSLT 1.0. Can anyone please guide me decoding the values between XML elements?

XML

<PdfPrinter>
<Reports>
<Report>
<BranchID>SA-02</BranchID>
<Trade_x0020_Date>2016-08-10T00:00:00</Trade_x0020_Date>
<Account_x0020_No>ABCDE01384</Account_x0020_No>
<CParty_x0020_ID/>
<Reference_x0020_No>TS-N-203-001</Reference_x0020_No>
<Source>N</Source>
<CDS_x0020_No>0000001387</CDS_x0020_No>
<Dealer_x0020_ID>1D01</Dealer_x0020_ID>
<Stock_x0020_ID>0400GA</Stock_x0020_ID>
<Price>0.123000</Price>
<Quantity>-1200</Quantity>
<Currency xmlns="Traded">USD</Currency>
<Nett_x0020_Amount xmlns="Traded">-86.600000</Nett_x0020_Amount>
<Currency xmlns="Local">USD</Currency>
<Nett_x0020_Amount xmlns="Local">-86.600000</Nett_x0020_Amount>
<Promo_x0020_Ind>N</Promo_x0020_Ind>
<Charge_x0020_ID xmlns="Brokerage">BRK-N</Charge_x0020_ID>
<Amount xmlns="Brokerage">50.000000</Amount>
<Charge_x0020_ID>CLR</Charge_x0020_ID>
<Amount xmlns="ClearingFee">10.000000</Amount>
<Charge_x0020_ID xmlns="ClearingFee">SDC</Charge_x0020_ID>
<Amount xmlns="StampDuty">1.000000</Amount>
<Charge_x0020_ID xmlns="StampDuty"/>
<Charge_x0020_ID xmlns="Penalty">0</Charge_x0020_ID>
<Amount xmlns="Penalty">0.000000</Amount>
<Tax_x0020_Amount>0.000000</Tax_x0020_Amount>
</Report>
</Reports>
</PdfPrinter>


XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ext="http://exslt.org/common"
xmlns:utilityExtension="pdfprinter:extensions:utility"
exclude-result-prefixes="msxsl utilityExtension">
<xsl:output method="xml" indent="yes"
omit-xml-declaration="yes" encoding="utf-8"/>

<xsl:template match="/">
<xsl:variable name="Brokerage">Brokerage</xsl:variable>
<xsl:variable name="ClearingFee">Clearing Fee</xsl:variable>
<xsl:variable name="StampDuty">Stamp Duty</xsl:variable>
<xsl:variable name="Penalty">Penalty</xsl:variable>
<xsl:variable name="OtherCharges">Other Charges</xsl:variable>
<xsl:variable name="columnFontSize">12pt</xsl:variable>
<xsl:variable name="columnFontType">Helvetica</xsl:variable>
<xsl:variable name="headerOrgName">Pn</xsl:variable>
<xsl:variable name="headerPrintedBy">Printed By</xsl:variable>
<xsl:variable name="headerPrintedDate">Printed Date</xsl:variable>
<xsl:variable name="headerReportID">Report ID</xsl:variable>
<xsl:variable name="headerLogo" select
="utilityExtension:MapPath('~/App_Data/Resources/IMAGES/logo.jpg')"/>

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<!-- defines the layout master -->
<fo:layout-master-set>
<fo:simple-page-master master-name="all-pages" page-width="894mm" page-height="1300pt" margin-top="10mm" margin-left="20mm" margin-right="20mm" margin-bottom="10mm">
<fo:region-body region-name="xsl-region-body" column-gap="0in" margin="0.8in" margin-left="0.7in" margin-top="2.3in" margin-bottom="1.8in" padding-top="0in"/>
<fo:region-before region-name="xsl-region-before" extent="75mm" />
<fo:region-after region-name="xsl-region-after" extent="7in"/>
</fo:simple-page-master>
<fo:page-sequence-master master-name="default-sequence">
<fo:single-page-master-reference master-reference="all-pages"/>
<fo:repeatable-page-master-reference master-reference="all-pages" />
</fo:page-sequence-master>
</fo:layout-master-set>
<!-- starts actual layout -->
<fo:page-sequence master-reference="default-sequence" initial-page-number="1" id="last-page">
<fo:static-content flow-name="xsl-region-before" width="100%" border-top-style="solid" border-top-color="rgb(192,192,192)" padding-top="1pt">
<fo:block>
<fo:table border-collapse="collapse" width="100%" table-layout="fixed" margin-top="-0.002in" padding-left="0.7in" margin-left="0in" border-spacing="5px" space-before.conditionality="retain" space-after.conditionality="retain" space-after="1em" space-before="1em">
<fo:table-column column-width="proportional-column-width(55)" column-number="1"/>
<fo:table-column column-width="proportional-column-width(45)" column-number="2"/>
<fo:table-column column-width="proportional-column-width(45)" column-number="3"/>
<fo:table-column column-width="proportional-column-width(45)" column-number="4"/>
<fo:table-column column-width="proportional-column-width(45)" column-number="5"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell width="50%" text-align="left" padding-left="2pt">
<fo:block color="rgb(0,0,128)" font-size="16pt" font-weight="bold">&#160;</fo:block>
<fo:block color="rgb(0,0,128)" font-weight="bold"></fo:block>
<fo:block padding-left="0in">
<fo:external-graphic src="{$headerLogo}"/>&#160;&#160;&#160;&#160;
<fo:block>
</fo:block>
</fo:block>
<fo:block color="rgb(0,0,128)" font-weight="bold">&#160;&#160;&#160;&#160;</fo:block>
<fo:block color="rgb(0,0,0)" font-weight="bold" font-family="{$columnFontType}" font-size="{$columnFontSize}">
<xsl:value-of select="$headerReportID"/> : <xsl:value-of select="/PdfPrinter/Reports/Header/ReportID" />
</fo:block>
</fo:table-cell>
<fo:table-cell></fo:table-cell>
<fo:table-cell></fo:table-cell>
<fo:table-cell></fo:table-cell>
<fo:table-cell width="100%" text-align="left">
<fo:block>&#160;&#160;&#160;&#160;</fo:block>
<fo:block>&#160;&#160;&#160;&#160;</fo:block>
<fo:block>&#160;&#160;&#160;&#160;</fo:block>
<fo:block>&#160;&#160;&#160;&#160;</fo:block>
<fo:block>&#160;&#160;&#160;&#160;</fo:block>
<fo:block text-align="left">
<fo:inline font-weight="bold" font-family="{$columnFontType}" font-size="{$columnFontSize}">
Page <fo:page-number/> of <fo:page-number-citation ref-id="last-page"/>
</fo:inline>
</fo:block>
<fo:block text-align="left">
<fo:inline font-weight="bold" font-family="{$columnFontType}" font-size="{$columnFontSize}">
<xsl:value-of select="$headerPrintedBy"/> : <xsl:value-of select="/PdfPrinter/Reports/Report/BrkChgID[(count(current()/preceding-sibling::*)+1)]/@id"/>
</fo:inline>
</fo:block>
<fo:block text-align="left">
<fo:inline font-weight="bold" font-family="{$columnFontType}" font-size="{$columnFontSize}">
<xsl:value-of select="$headerPrintedDate"/> : <xsl:value-of select="/PdfPrinter/Reports/Header/PrintedDate" />
</fo:inline>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
<fo:block/>
</fo:block>
<fo:block text-align="center">
<fo:table padding-left="0.7in" border-bottom-width="5pt" border-width="1pt" font-weight="bold" inline-progression-dimension="auto" width="100%">
<fo:table-column column-number="1"/>
<fo:table-body>
<fo:table-row border-bottom-color="rgb(0,0,255)" display-align="before">
<fo:table-cell border-bottom-width="3pt" border-bottom-style="solid" border-bottom-color="rgb(255,255,255)" padding="2pt" text-align="left">
<fo:block text-align="center" font-size="13pt" font-weight="bold" font-family="Helvetica" border-bottom="10pt "></fo:block>
<fo:block>
<xsl:value-of select="/PdfPrinter/Reports/Header/ReportTitle" />
</fo:block>
<fo:block border-bottom-width="8pt" text-align="center" background-color="black"/>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:block>
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<fo:block text-align="center">

<!-- table start -->
<fo:table role="html:table" table-layout="auto" inline-progression-dimension="auto" line-height="2.5" width="100%" border="0px" border-collapse="collapse" border-spacing="3px" border-style="outset hidden" space-before.conditionality="retain" space-after.conditionality="retain" space-after="1em" space-before="1em">
<!--table header-->
<xsl:for-each select="/PdfPrinter/Reports/Report[1]/*">
<fo:table-column column-width="proportional-column-width(4.77)" font-family="sans-serif" font-size="13pt" />
</xsl:for-each>
<fo:table-header>
<fo:table-row height="auto" color="#FFFFFF" background-color="#A1A1A1" text-align="center" font-weight="bold" font-family="sans-serif" font-size="13pt">
<fo:table-cell text-align="center" column-number="16" number-columns-spanned="3" border-after-width="1pt" border-after-style="solid">
<fo:block font-size="13pt" font-family="sans-serif">
<xsl:value-of select="$Brokerage"/>
</fo:block>
</fo:table-cell>
<fo:table-cell text-align="center" column-number="19" number-columns-spanned="2">
<fo:block font-size="13pt" font-family="sans-serif">
<xsl:value-of select="$ClearingFee"/>
</fo:block>
</fo:table-cell>
<fo:table-cell text-align="center" column-number="21" number-columns-spanned="2">
<fo:block font-size="13pt" font-family="sans-serif">
<xsl:value-of select="$StampDuty"/>
</fo:block>
</fo:table-cell>
<fo:table-cell text-align="center" column-number="23" number-columns-spanned="2">
<fo:block font-size="13pt" font-family="sans-serif">
<xsl:value-of select="$Penalty"/>
</fo:block>
</fo:table-cell>
<fo:table-cell text-align="center" column-number="25" number-columns-spanned="1">
<fo:block font-size="13pt" font-family="sans-serif">
<xsl:value-of select="$OtherCharges"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row height="auto" color="#FFFFFF" background-color="#A1A1A1" text-align="center" font-weight="bold" font-family="sans-serif" font-size="14pt">
<xsl:for-each select="/PdfPrinter/Reports/Report[1]/*">
<fo:table-cell text-align="center">
<fo:block font-size="13pt" font-family="sans-serif">
<xsl:value-of select="name()"/>
</fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
</fo:table-header>
<!--table body-->
<fo:table-body>
<xsl:for-each select="PdfPrinter/Reports/Report">
<fo:table-row display-align="before">
<xsl:for-each select="*">
<fo:table-cell text-align="center" border-top-color="rgb(255, 255, 255)" border-top-style="solid" border-width="1pt" padding="2pt">
<fo:block font-size="13pt" font-family="sans-serif">
<xsl:value-of select="."/>
</fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
<!-- table end -->
</fo:block>
<fo:block id="last-page"/>
</fo:flow>
</fo:page-sequence>
</fo:root>





The element Charge_x0020_ID actually is Charge ID, But currently my XSLT is printing the encoded value. I would like to print the column as Charge ID by decoding the special character. Any help is greatly appreciated.

Answer

Try this template call:

<!--xsl:value-of select="name()"/-->
<xsl:call-template name="replaceDecodedSpace">
    <xsl:with-param name="prmStr" select="local-name()"/>
</xsl:call-template>

...

<xsl:template name="replaceDecodedSpace">
    <xsl:param name="prmStr" select="''"/>
    <xsl:variable name="encodedSpace" select="'_x0020_'"/>
    <xsl:choose>
        <xsl:when test="contains($prmStr,$encodedSpace)">
            <xsl:variable name="beforeSpacePart" select="substring-before($prmStr,$encodedSpace)"/>
            <xsl:variable name="afterSpacePart" select="substring-after($prmStr,$encodedSpace)"/>
            <xsl:value-of select="concat($beforeSpacePart,'&#x20;')"/>
            <xsl:call-template name="replaceDecodedSpace">
                <xsl:with-param name="prmStr" select="$afterSpacePart"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$prmStr"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

The result:

  <fo:table-row height="auto"
                color="#FFFFFF"
                background-color="#A1A1A1"
                text-align="center"
                font-weight="bold"
                font-family="sans-serif"
                font-size="14pt">
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">BranchID</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Trade Date</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Account No</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">CParty ID</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Reference No</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Source</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">CDS No</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Dealer ID</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Stock ID</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Price</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Quantity</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Currency</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Nett Amount</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Currency</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Nett Amount</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Promo Ind</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Charge ID</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Amount</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Charge ID</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Amount</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Charge ID</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Amount</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Charge ID</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Charge ID</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Amount</fo:block>
     </fo:table-cell>
     <fo:table-cell text-align="center">
        <fo:block font-size="13pt" font-family="sans-serif">Tax Amount</fo:block>
     </fo:table-cell>
  </fo:table-row>

This template also works if element name contains multiple decoded spaces.