xslt - Is it possible to build an XML document from a list of XPath values and the XML XSD? -
i have list of name/value pairs map xml document. idea this, assign each of names xpath this:
account_number = 4294587576-32 = /my:myfields/my:customer/my:accountnumber customer_name = john smith = /my:myfields/my:customer/my:name customer_address = tampa, fl 33604 = /my:myfields/my:customer/my:address amount_due = 129.85 = /my:myfields/my:amountdue/my:amount days_past_due = 54 = /my:myfields/my:amountdue/my:dayspastdue
now, shouldn't able take information along complete sample of xml document or xsd , build xml document looks this:
<my:myfields> <my:customer> <my:name>john smith</my:name> <my:accountnumber>4294587576-32</my:accountnumber> <my:address>tampa, fl 33604</my:address> </my:customer> <my:amountdue> <my:dayspastdue>54</my:dayspastdue> <my:amount>129.85</my:amount> </my:amountdue> </my:myfields>
my question specific microsoft infopath because need take list of name/value pairs , build xml data document infopath form. technology performs translation not have microsoft. java or c++ best solution. can done using xslt processor apache's xalan?
here 1 suggestion, mention java , xslt wouldn't bother xalan , xslt 1.0 instead use saxon 9 , xslt 2.0. assuming have well-formed xml input sample , above mapping of values xpath expressions feed mapping xslt 2.0 stylesheet generates second stylesheet can applied input sample. assuming have file 'test2011101201.txt' as
account_number = 4294587576-32 = /my:myfields/my:customer/my:accountnumber customer_name = john smith = /my:myfields/my:customer/my:name customer_address = tampa, fl 33604 = /my:myfields/my:customer/my:address amount_due = 129.85 = /my:myfields/my:amountdue/my:amount days_past_due = 54 = /my:myfields/my:amountdue/my:dayspastdue
the stylesheet
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:axsl="http://www.w3.org/1999/xsl/transforma" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:my="http://example.com/my" version="2.0" exclude-result-prefixes="xs"> <xsl:param name="text-file" as="xs:string" select="'test2011101201.txt'"/> <xsl:variable name="lines" as="xs:string*" select="tokenize(unparsed-text($text-file), '\r?\n')[normalize-space()]"/> <xsl:output method="xml" indent="yes"/> <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/> <xsl:template match="/"> <axsl:stylesheet version="2.0"> <axsl:template match="@* | node()"> <axsl:copy> <axsl:apply-templates select="@*, node()"/> </axsl:copy> </axsl:template> <xsl:for-each select="$lines"> <xsl:variable name="tokens" select="tokenize(., '=')"/> <axsl:template match="{normalize-space($tokens[3])}"> <axsl:copy> <axsl:apply-templates select="@*"/> <axsl:text> <xsl:value-of select="replace($tokens[2], '(^\s+|\s+$)', '')"/> </axsl:text> </axsl:copy> </axsl:template> </xsl:for-each> </axsl:stylesheet> </xsl:template> </xsl:stylesheet>
can run produce second stylesheet
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:my="http://example.com/my" version="2.0"> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@*, node()"/> </xsl:copy> </xsl:template> <xsl:template match="/my:myfields/my:customer/my:accountnumber"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>4294587576-32</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="/my:myfields/my:customer/my:name"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>john smith</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="/my:myfields/my:customer/my:address"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>tampa, fl 33604</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="/my:myfields/my:amountdue/my:amount"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>129.85</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="/my:myfields/my:amountdue/my:dayspastdue"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>54</xsl:text> </xsl:copy> </xsl:template> </xsl:stylesheet>
which, when applied input sample like
<my:myfields xmlns:my="http://example.com/my"> <my:customer> <my:name></my:name> <my:accountnumber></my:accountnumber> <my:address></my:address> </my:customer> <my:amountdue> <my:dayspastdue></my:dayspastdue> <my:amount></my:amount> </my:amountdue> </my:myfields>
outputs
<?xml version="1.0" encoding="utf-8"?><my:myfields xmlns:my="http://example.com/my"> <my:customer> <my:name>john smith</my:name> <my:accountnumber>4294587576-32</my:accountnumber> <my:address>tampa, fl 33604</my:address> </my:customer> <my:amountdue> <my:dayspastdue>54</my:dayspastdue> <my:amount>129.85</my:amount> </my:amountdue> </my:myfields>
as said, requires xslt 2.0 , xslt 2.0 processor saxon 9 parse text file. , whoever authors text file needs make sure path expressions stylesheet maps xslt match patterns not ambiguous.
Comments
Post a Comment