xslt node-set use in key function - find children

2019-09-02 19:55发布

问题:

I have an XML File.

<?xml version="1.0" encoding="UTF-8"?>
<XML>   
    <Items>
        <ItemRef ID='answer'></ItemRef>
        <ItemRef ID='number'></ItemRef>
        <ItemRef ID='delete'></ItemRef>
        <ItemRef ID='remove'></ItemRef>
    </Items>
    <ItemDef ID='anwser'></ItemDef>
    <ItemDef ID='number'>
        <EnumItemRef EID = '1'></EnumItemRef>
    </ItemDef>
    <ItemDef ID='delete'>
        <EnumItemRef EID = '2'></EnumItemRef>
    </ItemDef>        
    <ItemDef ID='remove'></ItemDef>
    <EnumItem EID="1">
        <EnumItem Value = '1'></EnumItem>
        <EnumItem Value = '2'></EnumItem>
        <EnumItem Value = '3'></EnumItem>
    </EnumItem>
    <EnumItem EID="2">
        <EnumItem Value = 'yes'></EnumItem>
        <EnumItem Value = 'no'></EnumItem>
    </EnumItem>
</XML>

what I need to transform into a XML file like this:

<?xml version="1.0" encoding="UTF-8"?>
<XML>   
    <Items>
        <ItemRef ID='newid_answer'></ItemRef>
        <ItemRef ID='newid_number'></ItemRef>
    </Items>
    <ItemDef ID='newid_answer'></ItemDef>
    <ItemDef ID='newid_number'>
        <EnumItemRef EID = '1'></EnumItemRef>
    </ItemDef>        
    <EnumItem EID="1">
        <EnumItem Value = '1'></EnumItem>
        <EnumItem Value = '2'></EnumItem>
        <EnumItem Value = '3'></EnumItem>
    </EnumItem>
</XML>

I have following XSLT 1.0 file. With this I'm able to create the Items and ItemDefs I want. I'm just missing an idea how to find the corresonding EnumItemRef and EnumItem. Either I have to check if the current ItemDef has a child called EnumItemRef, or I could solve it with a key function (what would maybe be the better solution). I tried both but I can't make it work.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:variable name="Items">
    <Item ID="answer">newid_answer</Item>
    <Item ID="number">newid_number</Item>
</xsl:variable>

<xsl:key name="get_Enum_by_ID" match="/XML/ItemDef/EnumItemRef" use="/XML/ItemDef/@ID" />

<xsl:template match="XML">
    <xsl:copy>
        <xsl:apply-templates />
        <xsl:call-template name="Items" />
        <xsl:call-template name="ItemDef" />
    </xsl:copy>
</xsl:template>

<xsl:template name="Items">
        <xsl:element name="Items">
        <xsl:for-each select="exslt:node-set($Items)/Item">
            <xsl:element name="ItemRef">
                <xsl:attribute name="ID"><xsl:value-of select="text()"/></xsl:attribute>
            </xsl:element>
        </xsl:for-each>
    </xsl:element>
</xsl:template>

<xsl:template name="ItemDef">
    <xsl:for-each select="exslt:node-set($Items)/Item">
        <xsl:element name="ItemDef">
            <xsl:attribute name="ID"><xsl:value-of select="text()" /></xsl:attribute>
        </xsl:element>
        <-- how can I find the corresponding EnumitemRef and EnumItem? -->
        <xsl:copy-of select="key('get_Enum_by_ID', @ID)" /> 

    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

I would be happy if anyone could give me an idea how to solve this.

回答1:

Although this is not simple, I think you are making it much more complicated than it needs to be. Try the following stylesheet (not tested very thoroughly):

XSLT 1.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">

<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="Items">
    <Item ID="answer">newid_answer</Item>
    <Item ID="number">newid_number</Item>
</xsl:variable>

<xsl:key name="ItemDef-by-ID" match="ItemDef" use="@ID"/>
<xsl:key name="EnumItem-by-EID" match="EnumItem" use="@EID"/>

<xsl:variable name="xml" select="/"/>

<xsl:template match="/XML">
    <xsl:copy>
        <Items>
            <xsl:copy-of select="$Items" /> 
        </Items>
        <xsl:apply-templates select="exsl:node-set($Items)/Item"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Item">
    <xsl:variable name="item-id" select="@ID"/>

    <xsl:variable name="EnumItemRef">
        <xsl:for-each select="$xml">
            <xsl:copy-of select="key('ItemDef-by-ID', $item-id)/EnumItemRef"/>
        </xsl:for-each>
    </xsl:variable>

    <xsl:variable name="EID" select="exsl:node-set($EnumItemRef)/EnumItemRef/@EID"/>

    <ItemDef ID="{.}">
        <xsl:copy-of select="$EnumItemRef"/>
    </ItemDef>
    <xsl:for-each select="$xml">
        <xsl:copy-of select="key('EnumItem-by-EID', $EID)"/>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Note: when you are in the context of a node-set created by the exslt:node-set() function, you cannot use a key() to lookup data in the source XML document, unless you switch the context to the source document first.