I am new to XSLT have a XML that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<FileHeaderRecord>
<FormatCode>1</FormatCode>
<ImmediateDestinationName>MEPS</ImmediateDestinationName
<CompanyBatchHeader>
<OriginatorStatusCode>1</OriginatorStatusCode>
<OriginatingFIIdentification>10000214</OriginatingFIIdentification>
<BatchNumber>0000004</BatchNumber>
<recordvalues>22100002225201198876900</recordvalues>
<ammendvalues>23</ammendvalues>
<ammendvalues>23439358</ammendvalues>
<recordvalues>1100002143439359</recordvalues>
<ammendvalues>0013439359</ammendvalues>
<ammendvalues>9359</ammendvalues>
</CompanyBatchHeader>
<CompanyBatchTrailer>
<Sderviceclasscode>220000003</Sderviceclasscode>
</CompanyBatchTrailer>
</FileHeaderRecord>
I want to add a parent called data to recordvalues and ammendvalues(also CompanyBatchHeader can appear more than one time and also number of ammendvalues after recordvalues are not fixed)making output file like:
<?xml version="1.0" encoding="UTF-8"?>
<FileHeaderRecord>
<FormatCode>1</FormatCode>
<ImmediateDestinationName>MEPS</ImmediateDestinationName
<CompanyBatchHeader>
<OriginatorStatusCode>1</OriginatorStatusCode>
<OriginatingFIIdentification>10000214</OriginatingFIIdentification>
<BatchNumber>0000004</BatchNumber>
<data>
<recordvalues>22100002225201198876900</recordvalues>
<ammendvalues>23</ammendvalues>
<ammendvalues>23439358</ammendvalues>
</data>
<data>
<recordvalues>1100002143439359</recordvalues>
<ammendvalues>0013439359</ammendvalues>
</data>
</CompanyBatchHeader>
<CompanyBatchTrailer>
<Sderviceclasscode>220000003</Sderviceclasscode>
</CompanyBatchTrailer>
</FileHeaderRecord>
I have tried few xslt none of them worked in the way i wanted. Please can someone help me in the same?
I have tried following xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<FileHeaderRecord>
<xsl:for-each select="FileHeaderRecord/CompanyBatchHeader/recordvalues">
<xsl:variable name="ammend_count" select="count(preceding-sibling::recordvalues) + 1"/>
<data>
<xsl:copy-of select="."/>
<xsl:copy-of select="following-sibling::ammendvalues[count(preceding-sibling::recordvalues) = $ammend_count]"/>
</data>
</xsl:for-each>
</FileHeaderRecord>
</xsl:template>
</xsl:stylesheet>
It is able to add data tag across recordvalues and ammendvalues but it is omitting everything else. And I don't know how to solve this problem.
答案 0 :(得分:0)
假设您限于XSLT 1.0,建议您尝试这种方式。这利用 key 来将ammendvalues
与前一个recordvalues
同级关联:
XSLT 1.0
<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="*"/>
<xsl:key name="ammend-by-record" match="ammendvalues" use="generate-id(preceding-sibling::recordvalues[1])" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="recordvalues">
<data>
<!-- copy self -->
<xsl:copy-of select="."/>
<!-- copy adjacent ammendvalues -->
<xsl:copy-of select="key('ammend-by-record', generate-id())"/>
</data>
</xsl:template>
<!-- remove ammendvalues at their original location -->
<xsl:template match="ammendvalues"/>
</xsl:stylesheet>