xml - XSLT: Splitting a string and doing something with each character -
i'm xslt newbie. i've gone through tutorials , i've been able 80% of want, xml document. however, stuck on something. in xml document, have attributes consist of values "era", "eda", "edar", , on. these attributes consist of combinations of letters e, d, a, , r. e, d, a, , r map edit, delete, add, , read.
if doing imperatively, split string component characters , check each character see if should output edit, delete, add, or read. how can similar in xslt? thinking of using length , substring functions , making loop of sort.
inline (or external) map:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:local="http://localhost"> <local:map letter="e" text="edit"/> <local:map letter="d" text="delete"/> <local:map letter="a" text="add"/> <local:map letter="r" text="read"/> <xsl:template match="test"> <xsl:copy> <xsl:apply-templates select="document('')/*/local:map[ contains(current(),@letter) ]/@text" mode="sequence"/> </xsl:copy> </xsl:template> <xsl:template match="node()|@*" mode="sequence"> <xsl:value-of select="concat(substring(' ', 1 div (position()!=1)),.)"/> </xsl:template> </xsl:stylesheet> output:
<test>edit delete add read</test> sequence of xsl:if instructions:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="test"> <xsl:copy> <xsl:if test="contains(.,'e')">edit </xsl:if> <xsl:if test="contains(.,'d')">delete </xsl:if> <xsl:if test="contains(.,'a')">add </xsl:if> <xsl:if test="contains(.,'r')">read </xsl:if> </xsl:copy> </xsl:template> </xsl:stylesheet> output:
<test>edit delete add read </test>
Comments
Post a Comment