Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

XSLT transform

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please suggest.
My XML data :
<XML>
<LogLevel>info</LogLevel>
<Content>
<Msg>
<AccountNo>6666</AccountNo>
<ApplName>Testing</ApplName>
</Msg>
</Content>
<data><AccountNo>555555</AccountNo></data>
<AccountNo>444444444</AccountNo>
<Date>20140909</Date>
</XML>


I want to mask all the AccountNo to the output as below.

<XML>
<LogLevel>info</LogLevel>
<Content>
<Msg>
<AccountNo>******66</AccountNo>
<ApplName>Testing</ApplName>
</Msg>
</Content>
<data><AccountNo>******5555</AccountNo></data>
<AccountNo>******444</AccountNo>
<Date>20140909</Date>
</XML>



Java code :




XSLT : I am not good at XSLT i am learning it.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="AccountNo">
<xsl:variable name="acct-num" select="substring-before(substring-after(.,'AccountNo'), '/AccountNo')" />
<xsl:value-of select="concat('************', substring($acct-num, string-length($acct-num) - 2))" />

</xsl:template>

</xsl:stylesheet>

Could you please let me know what should be the XSLT..

I am getting wrong output :

<?xml version="1.0" encoding="UTF-8"?>
<XML>
<LogLevel>info</LogLevel>
<Content>
<Msg>************<ApplName>Testing</ApplName>
</Msg>
</Content>
<data>************</data>************<Date>20140909</Date>
</XML>
 
Ranch Hand
Posts: 734
7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main problem is that the meaning of the dot or the context of the match for a template is misunderstood. That reflects fully in the construction of the variable $acct-num. The dot in a string function would be the text context of the node in context. There isn't any trace of 'AccountNo', the tag name, anymore. The AccountNo element, on the other hand, has to be re-constructed through xsl:copy.

This is how it should look like, with some improvements like in case where the account number being of length less than or equal to 2 (you then don't want to show anything, do you? because it would reveal too much). This is how you mask and then produce a fixed length output. I hope that gives you an instructive implementation on multiple aspect --- you've to discover them yourself.
 
This tiny ad is guaranteed to be gluten free.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic