XML,XSLT转换和查找生成的ID

时间:2010-12-30 14:18:27

标签: xml xslt

我有一个xml文件并使用xslt转换,我需要生成以下xml文件。

<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>

使用xslt转换,我能够生成实体节点。要生成关系节点,我需要生成UniqueID。 实体节点中元素的生成唯一ID不存在于原始xml文件中,而是使用XSL文件生成。 为了在XSLT代码之后生成唯一id,使用..

<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>

对于Categorie,XLST模板类似于classificate模板。在原始XML文件中,类别节点嵌套在classificatie元素下。

在新生成的XML中,我需要在entieties元素下创建单独的元素。并且这些之间的关系需要在“关系”元素下建立。

我能够将节点生成为实体下的单独节点。并且我无法使用新生成的ID在“关系”节点下生成元素。

简而言之:使用非嵌套元素转换父子关系(嵌套关系)。 -Rajesh

源XML和xslt文件在以下位置压缩 Zip Files

从链接更新:减少输入样本

<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>

1 个答案:

答案 0 :(得分:1)

此样式表:

<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>

使用上一个输入样本,输出:

<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>