Create a concatenated, comma separated list from XML nodes at different levels in the XML tree using XPath/XSLT -
i asked generate comma separated list generated xml document. when discounts consisted of nodes grouped under vehicle node worked ok (i have limited understanding of xpath). asked add 1 more discount located in different section of xml. using approach can't seem value of node need. clarification, don't create xml file, generated vendor system , 5k lines long. used post basis creating list.
desired output (if node in loop != '0' or paymentplancd != 'paidinfull' add list - psuedo-code)
multi-car, homeowner, affinity, in-agency transfer, advanced purchase, incident free, annual mileage, paid in full discount
i getting comma @ end changed whitespace based approach seemed work (method 2). rather have comma based approach (method 1) though. below sample of xml document , code i'm trying make work. if there easier or more eloquent solution, i'm ears.
sample xml (@pval stands "proposal value")
<datastore> <session> <data> <policy> <perspolicy> <paymentoption> <paymentplancd>paidinfull</paymentplancd> </paymentoption> </perspolicy> <line> <vehicle id="1"> <discountpremmulticar pval="multi-car">80</discountpremmulticar> <discountpremhomeowners pval="homeowner">63</discountpremhomeowners> <discountpremaffinity pval="affinity">0</discountpremaffinity> <discountpreminagencytransfer pval="in-agency transfer">57</discountpreminagencytransfer> <discountpremadvpurchase pval="advanced purchase">15</discountpremadvpurchase> <discountpremincidentfree pval="incident free">30</discountpremincidentfree> <discountpremannualmileage pval="annual mileage">0</discountpremannualmileage> </vehicle> <vehicle id="2"> <discountpremmulticar pval="multi-car">80</discountpremmulticar> <discountpremhomeowners pval="homeowner">63</discountpremhomeowners> <discountpremaffinity pval="affinity">0</discountpremaffinity> <discountpreminagencytransfer pval="in-agency transfer">57</discountpreminagencytransfer> <discountpremadvpurchase pval="advanced purchase">15</discountpremadvpurchase> <discountpremincidentfree pval="incident free">30</discountpremincidentfree> <discountpremannualmileage pval="annual mileage">0</discountpremannualmileage> </vehicle> </line> </policy> </data> </session> </datastore>
xslt/xpath
<xsl:for-each select="//session/data/policy/line/vehicle"> <xsl:call-template name="vehiclediscounts" /> </xsl:for-each>
method 1
<xsl:template name="vehiclediscounts"> <xsl:for-each select="discountpremadvpurchase/text() | discountpremaffinity/text() | discountpremannualmileage/text() | discountprememployee/text() | discountpremhomeowners/text() | discountpreminagencytransfer/text() | discountpremmulticar/text() | discountpremmultipolicy/text() | discountpremincidentfree/text() | ../../../perspolicy/paymentoptions/paymentplancd/text()"> <xsl:if test="../. != '0'"> <xsl:value-of select="../@pval"/> <xsl:if test="not(position() = last())">, </xsl:if> </xsl:if> </xsl:for-each> </xsl:template>
method 2
<xsl:template name="vehiclediscounts"> <xsl:for-each select="discountpremadvpurchase/text() | discountpremaffinity/text() | discountpremannualmileage/text() | discountprememployee/text() | discountpremhomeowners/text() | discountpreminagencytransfer/text() | discountpremmulticar/text() | discountpremmultipolicy/text() | discountpremincidentfree/text() | ../../../perspolicy/paymentoptions/paymentplancd/text()"> <xsl:choose> <xsl:when test="name(../.) = 'discountpremadvpurchase'"> <xsl:if test="../. != '0'"> advance purchase <xsl:if test="not(position() = last())">    </xsl:if> </xsl:if> </xsl:when> <xsl:when test="name(../.) = 'discountpremaffinity'"> <xsl:if test="../. != '0'"> affinity <xsl:if test="not(position() = last())">    </xsl:if> </xsl:if> </xsl:when> <xsl:when test="name(../.) = 'discountprememployee'"> <xsl:if test="../. != '0'"> group <xsl:if test="not(position() = last())">    </xsl:if> </xsl:if> </xsl:when> <xsl:when test="name(../.) = 'discountpremhomeowners'"> <xsl:if test="../. != '0'"> homeowners <xsl:if test="not(position() = last())">    </xsl:if> </xsl:if> </xsl:when> <xsl:when test="name(../.) = 'discountpreminagencytransfer'"> <xsl:if test="../. != '0'"> in-agency transfer <xsl:if test="not(position() = last())">    </xsl:if> </xsl:if> </xsl:when> <xsl:when test="name(../.) = 'discountpremmulticar'"> <xsl:if test="../. != '0'"> multi-car <xsl:if test="not(position() = last())">    </xsl:if> </xsl:if> </xsl:when> <xsl:when test="name(../.) = 'discountpremmultipolicy'"> <xsl:if test="../. != '0'"> multi-policy <xsl:if test="not(position() = last())">    </xsl:if> </xsl:if> </xsl:when> <xsl:when test="name(../.) = 'discountpremincidentfree'"> <xsl:if test="../. != '0'"> incident-free <xsl:if test="not(position() = last())">    </xsl:if> </xsl:if> </xsl:when> <xsl:when test="name(../.) = 'discountpremannualmileage'"> <xsl:if test="../. != '0'"> annual mileage <xsl:if test="not(position() = last())">    </xsl:if> </xsl:if> </xsl:when> <!-- have no clue --> <xsl:when test="name(../.) = 'paymentplancd'"> paid in full discount <xsl:if test="../. != '0'"> paid in full discount <xsl:if test="not(position() = last())">    </xsl:if> </xsl:if> </xsl:when> </xsl:choose> </xsl:for-each> </xsl:template>
how method 1 working in scenario? help.
i not sure difficulty here is. try following stylesheet , tell - in simple, non-technical language - (if anything) missing?
xslt 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text" encoding="utf-8"/> <xsl:template match="/"> <xsl:text>multi-car,homeowner,affinity,in-agency transfer,advanced purchase,incident free,annual mileage,paid in full discount </xsl:text> <xsl:for-each select="datastore/session/data/policy/line/vehicle"> <xsl:for-each select="*"> <xsl:value-of select="."/> <xsl:text>,</xsl:text> </xsl:for-each> <xsl:value-of select="../../perspolicy/paymentoption/paymentplancd"/> <xsl:if test="not(position()=last())"> <xsl:text> </xsl:text> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment