XML, XSLT transformation and lookup for generated ids
I have an xml file and using xslt transformation, I need to genera开发者_运维问答te following xml file.
<root>
<Entities>
<Classifications>
<classificatie UniqueID="1">
<Name>standaard1</Name>
</classificatie>
<classificatie UniqueID="2">
<Name>bdi</Name>
</classificatie>
<classificatie UniqueID="3">
<Name>lokaal1</Name>
</classificatie>
</Classifications>
<Categories>
<categorie UniqueID="1" id="D0001" super-id="" volgnummer="">
<Name>Beleid vast-/opstellen en Bestuur</Name>
</categorie>
<categorie UniqueID="2" id="D0002" super-id="" volgnummer="">
<Name>Beleid uitvoeren</Name>
</categorie>
<categorie UniqueID="3" id="D0003" super-id="" volgnummer="">
<Name>Beheer en Handhaving</Name>
</categorie>
</Categories>
</Entities>
<Relations>
<Classification_Category ClassficationID="1" CategoryID="2" />
</Relations>
</root>
Using xslt transformation, I am able to generate the entities node. To generate the relations node, I need to generated UniqueID. The generated uniqueID for elements in entity node are not present in original xml file but generated using XSL file. For generating the unique id following XSLT code is used ..
<xsl:template match="classificatie">
<xsl:variable name="vNum1">
<xsl:number level="any" count="classificatie"/>
</xsl:variable>
<classificatie>
<xsl:attribute name="UniqueID">
<xsl:value-of select="$vNum1"/>
</xsl:attribute>
<Name>
<xsl:value-of select="@id"/>
</Name>
</classificatie>
</xsl:template>
For Categorie also, XLST template is similar to classificate template. In original XML file categorie node is nested under classificatie element.
In newly generated XML, I need to make it separate elements under entieties elements. And the relation between these needs to be established under "Relations" elements.
I am able to generate the nodes as separate node under entities. And i am not able to generate elements under "Relations" node using newly generated ids.
In short : Converting parent-child relation (nested relation) with non-nested elements. -Rajesh
The source XML and xslt files are zipped at following location Zip Files
Update from link: Reduce input sample
<root>
<classificaties>
<classificatie id="standard">
<categorieen>
<categorie id="D0001" super-id="">Category1</categorie>
<categorie id="D0002" super-id="">Category2</categorie>
<categorie id="D0003" super-id="">Category3</categorie>
<categorie id="D0004" super-id="D0001">Category1.1</categorie>
<categorie id="D0005" super-id="D0001">Category1.2</categorie>
<categorie id="D0007" super-id="D0002">Category2.1</categorie>
<categorie id="D0021" super-id="D0003">Category3.1</categorie>
<categorie id="D0025" super-id="D0002">Category2.2</categorie>
</categorieen>
</classificatie>
<classificatie id="bdi">
<categorieen>
<categorie id="MLF0000002"
super-id="" volgnummer="1">Test 1</categorie>
<categorie id="MLF0000003"
super-id="" volgnummer="2">Test 2</categorie>
<categorie id="MLF0000017"
super-id="MLF0000003" volgnummer="1">Test 17</categorie>
<categorie id="MLF0000020"
super-id="MLF0000002" volgnummer="2">Test 20</categorie>
<categorie id="MLF0000021"
super-id="MLF0000002" volgnummer="3">Test 21</categorie>
<categorie id="MLF0000025"
super-id="MLF0000003" volgnummer="2">Test 25</categorie>
<categorie id="MLF0000027"
super-id="MLF0000003" volgnummer="3">Test 27</categorie>
<categorie id="MLF0000030"
super-id="MLF0000003" volgnummer="4">Test 30</categorie>
<categorie id="MLF0000031"
super-id="MLF0000003" volgnummer="5">Test 31</categorie>
</categorieen>
</classificatie>
</classificaties>
</root>
This stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<root>
<Entities>
<Classifications>
<xsl:apply-templates select="*/*"/>
</Classifications>
<Categories>
<xsl:apply-templates select="*/*/*/*"/>
</Categories>
</Entities>
<Relations>
<xsl:apply-templates select="*/*/*/*" mode="relations"/>
</Relations>
</root>
</xsl:template>
<xsl:template match="classificatie">
<classificatie UniqueID="{position()}">
<name>
<xsl:value-of select="@id"/>
</name>
</classificatie>
</xsl:template>
<xsl:template match="categorie">
<categorie UniqueID="{position()}">
<xsl:copy-of select="@*"/>
<name>
<xsl:value-of select="."/>
</name>
</categorie>
</xsl:template>
<xsl:template match="categorie" mode="relations">
<xsl:variable name="vClass">
<xsl:number count="classificatie"/>
</xsl:variable>
<Classification_Category ClassficationID="{$vClass}"
CategoryID="{position()}"/>
</xsl:template>
</xsl:stylesheet>
With last input sample, output:
<root>
<Entities>
<Classifications>
<classificatie UniqueID="1">
<name>standard</name>
</classificatie>
<classificatie UniqueID="2">
<name>bdi</name>
</classificatie>
<classificatie UniqueID="3">
<name>lokal</name>
</classificatie>
</Classifications>
<Categories>
<categorie UniqueID="1" id="D0001" super-id="">
<name>Category1</name>
</categorie>
<categorie UniqueID="2" id="D0002" super-id="">
<name>Category2</name>
</categorie>
<categorie UniqueID="3" id="D0003" super-id="">
<name>Category3</name>
</categorie>
<categorie UniqueID="4" id="D0004" super-id="D0001">
<name>Category1.1</name>
</categorie>
<categorie UniqueID="5" id="D0005" super-id="D0001">
<name>Category1.2</name>
</categorie>
<categorie UniqueID="6" id="D0007" super-id="D0002">
<name>Category2.1</name>
</categorie>
<categorie UniqueID="7" id="D0021" super-id="D0003">
<name>Category3.1</name>
</categorie>
<categorie UniqueID="8" id="D0025" super-id="D0002">
<name>Category2.2</name>
</categorie>
<categorie UniqueID="9" id="MLF0000002" super-id=""
volgnummer="1">
<name>Test 1</name>
</categorie>
<categorie UniqueID="10" id="MLF0000003" super-id=""
volgnummer="2">
<name>Test 2</name>
</categorie>
<categorie UniqueID="11" id="MLF0000017" super-id="MLF0000003"
volgnummer="1">
<name>Test 17</name>
</categorie>
<categorie UniqueID="12" id="MLF0000020" super-id="MLF0000002"
volgnummer="2">
<name>Test 20</name>
</categorie>
<categorie UniqueID="13" id="MLF0000021" super-id="MLF0000002"
volgnummer="3">
<name>Test 21</name>
</categorie>
<categorie UniqueID="14" id="MLF0000025" super-id="MLF0000003"
volgnummer="2">
<name>Test 25</name>
</categorie>
<categorie UniqueID="15" id="MLF0000027" super-id="MLF0000003"
volgnummer="3">
<name>Test 27</name>
</categorie>
<categorie UniqueID="16" id="MLF0000030" super-id="MLF0000003"
volgnummer="4">
<name>Test 30</name>
</categorie>
<categorie UniqueID="17" id="MLF0000031" super-id="MLF0000003"
volgnummer="5">
<name>Test 31</name>
</categorie>
</Categories>
</Entities>
<Relations>
<Classification_Category ClassficationID="1" CategoryID="1" />
<Classification_Category ClassficationID="1" CategoryID="2" />
<Classification_Category ClassficationID="1" CategoryID="3" />
<Classification_Category ClassficationID="1" CategoryID="4" />
<Classification_Category ClassficationID="1" CategoryID="5" />
<Classification_Category ClassficationID="1" CategoryID="6" />
<Classification_Category ClassficationID="1" CategoryID="7" />
<Classification_Category ClassficationID="1" CategoryID="8" />
<Classification_Category ClassficationID="2" CategoryID="9" />
<Classification_Category ClassficationID="2" CategoryID="10" />
<Classification_Category ClassficationID="2" CategoryID="11" />
<Classification_Category ClassficationID="2" CategoryID="12" />
<Classification_Category ClassficationID="2" CategoryID="13" />
<Classification_Category ClassficationID="2" CategoryID="14" />
<Classification_Category ClassficationID="2" CategoryID="15" />
<Classification_Category ClassficationID="2" CategoryID="16" />
<Classification_Category ClassficationID="2" CategoryID="17" />
</Relations>
</root>
精彩评论