Мне нужно преобразовать плоский XML в иерархическую XML-структуру. Целевые элементы должны отображаться на другом уровне, чем исходный XML.Я заканчиваю в цикле XML несколько раз, чтобы создать целевой XML.Использование // с именем атрибута также не является хорошей идеей, поскольку xml может быть огромным, и это может привести к снижению производительности.
Я хотел бы понять, можно ли написать xslt лучшеway;
Я попытался продемонстрировать простой пример ниже.
Любые идеи или помощь приветствуются.
input xml:
<result>
<program>
<c i='0' d='Policy'>
<c i='14' d='LOB'>
<m i='CR TOT TERR TERM PREM' d='CR TOT TERR TERM PREM' v='-17277' />
<m i='CR TOT TERR NET PREM' d='CR TOT TERR NET PREM' v='-14935' />
<m i='CR TOT TERR GAP PREM' d='CR TOT TERR GAP PREM' v='294' />
<m i='CR TOT TERR FINAL ADJ PREM' d='CR TOT TERR FINAL ADJ PREM'
v='-17571' />
<m i='CR QUOTE OPTION ID' d='CR QUOTE OPTION ID'
v='Quote0001' />
<m i='CR TOT TERR COMMISSION' d='CR TOT TERR COMMISSION' v='-2635.65' />
<m i='CR TOT TERR ACTUAL PREM' d='CR TOT TERR ACTUAL PREM' v='-17571' />
<m i='CR TOT TERM PREM' d='CR TOT TERM PREM' v='-881154' />
<m i='CR TOT TAXES AND SRCHRG' d='CR TOT TAXES AND SRCHRG' v='-892.14' />
<m i='CR TOT STATE SRCHRG' d='CR TOT STATE SRCHRG' v='-896.14' />
<m i='CR TOT PREM ADJ' d='CR TOT PREM ADJ' v='0' />
<m i='CR TOT PREM' d='CR TOT PREM' v='-897026.14' />
<m i='CR TOT NET PREM' d='CR TOT NET PREM' v='-761713' />
<m i='CR TOT GAP PREM' d='CR TOT GAP PREM' v='14980' />
<m i='CR TOT FINAL ADJ PREM' d='CR TOT FINAL ADJ PREM' v='-896134' />
<m i='CR TOT COMMISSION' d='CR TOT COMMISSION' v='-134420.1' />
<m i='CR TOT ADDL COV TERM PREM' d='CR TOT ADDL COV TERM PREM' v='-879465' />
<m i='CR TOT ADDL COV NET PREM' d='CR TOT ADDL COV NET PREM' v='-760253.59999999998' />
<m i='CR TOT ADDL COV GAP PREM' d='CR TOT ADDL COV GAP PREM' v='14951' />
<m i='CR TOT ADDL COV FINAL ADJ PREM' d='CR TOT ADDL COV FINAL ADJ PREM'
v='-894416' />
<m i='CR TOT ADDL COV COMMISSION' d='CR TOT ADDL COV COMMISSION'
v='-134162.40000000002' />
<m i='CR TOT ADDL COV ACTUAL PREM' d='CR TOT ADDL COV ACTUAL PREM'
v='-894416' />
<m i='CR TOT ACTUAL PREM' d='CR TOT ACTUAL PREM' v='-896134' />
<m i='CR STATE SRCHRG NAME' d='CR STATE SRCHRG NAME' v='FL Comml Prop Fire Marshall' />
<m i='CR FL POLICY SRCHRG NAME' d='CR FL POLICY SRCHRG NAME' v='FL Emergency Mgmt Praparedness' />
<m i='CR FL POLICY SRCHRG' d='CR FL POLICY SRCHRG' v='4' />
<m i='CR EMPL THEFT ANNUAL RATE' d='CR EMPL THEFT ANNUAL RATE' v='1.144' />
<m i='CR EMPL THEFT ANNUAL PREM' d='CR EMPL THEFT ANNUAL PREM' v='15401' />
</c>
</c>
</program>
</result>
xslt:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns="urn:company:esb:services:Pricing:v01">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes">
</xsl:output>
<xsl:strip-space elements="*"/>
<xsl:template match="/result/program/c">
<xsl:element name="pricing-response">
<dummy>
<policy>
<xsl:for-each select="c">
<xsl:variable name='strComp'>
<xsl:value-of select='@i'/>
</xsl:variable>
<xsl:if test="@d ='CrimeLine' ">
<policy-output>
<xsl:for-each select="m">
<xsl:variable name='strComp'>
<xsl:value-of select='@i'/>
</xsl:variable>
<xsl:if test="$strComp ='CR QUOTE OPTION ID' ">
<quote-option-id>
<xsl:value-of select='@v'/>
</quote-option-id>
</xsl:if>
<xsl:if test="$strComp ='CR TOT TAXES AND SRCHRG' ">
<total-taxsurcharge-amt>
<amount>
<xsl:value-of select='@v'/>
</amount>
</total-taxsurcharge-amt>
</xsl:if>
<xsl:if test="$strComp ='CR TOT PREM' ">
<total-premium-amt>
<amount>
<xsl:value-of select='@v'/>
</amount>
</total-premium-amt>
</xsl:if>
</xsl:for-each>
<taxsurcharge-summary>
<taxsurcharge>
<taxsurcharge-identifier>State</taxsurcharge-identifier>
<xsl:for-each select="m">
<xsl:variable name='strComp'>
<xsl:value-of select='@i'/>
</xsl:variable>
<xsl:if test="$strComp ='CR TOT STATE SRCHRG' ">
<taxsurcharge-amt>
<amount>
<xsl:value-of select='@v'/>
</amount>
</taxsurcharge-amt>
</xsl:if>
<xsl:if test="$strComp ='CR STATE SRCHRG NAME' ">
<taxsurcharge-name>
<xsl:value-of select='@v'/>
</taxsurcharge-name>
</xsl:if>
<xsl:if test="$strComp ='CR STATE SRCHRG CODE' ">
<taxsurcharge-code>
<xsl:value-of select='@v'/>
</taxsurcharge-code>
</xsl:if>
</xsl:for-each>
</taxsurcharge>
</taxsurcharge-summary>
</policy-output>
</xsl:if>
</xsl:for-each>
</policy>
</dummy>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
выходной xml:
<?xml version="1.0" encoding="UTF-8"?>
<pricing-response xmlns="urn:company:esb:services:Pricing:v01">
<dummy>
<policy>
<policy-output>
<quote-option-id>Quote0001</quote-option-id>
<total-taxsurcharge-amt>
<amount>-892.14</amount>
</total-taxsurcharge-amt>
<total-premium-amt>
<amount>-897026.14</amount>
</total-premium-amt>
<taxsurcharge-summary>
<taxsurcharge>
<taxsurcharge-identifier>State</taxsurcharge-identifier>
<taxsurcharge-amt>
<amount>-896.14</amount>
</taxsurcharge-amt>
<taxsurcharge-name>FL Comml Prop Fire Marshall</taxsurcharge-name>
</taxsurcharge>
</taxsurcharge-summary>
</policy-output>
</policy>
</dummy>
</pricing-response>