从父级检索属性值并将其显示为子级中的属性

时间:2012-06-26 20:01:12

标签: xml xslt

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">    
<Package ID="b7137f22-31da-4d90-971c-b6baad654c96" BusinessID="P_000137" Path="/Package/Product/Launch_Entity" xsi:type="Package_Private_Line">
    <Category_ID Product_Line_ID="63">63</Category_ID>
    <Compatibility_Rules ID="32fd84b4-7b9b-40e2-8265-8e2e4f856a8d">
        <Rule_Event ID="992f04cb-6bd1-42bb-9578-87793dd38aca">
            <Name>Entity Selection</Name>
        </Rule_Event><Rule_Name>Private Line Exclusions</Rule_Name>
        <Rule_Statement ID="d0cc0700-2199-4a51-af68-72dfe27d878f">
            <Description>IF package is Private line THEN exclude Ethernet Switched Access (ESA)</Description>
            <Actions ID="d8165dda-7f8b-4c44-8400-eb081c0ec79c">
                <Then>THEN exclude Ethernet Switched Access (ESA</Then>
                <Entity>PSpec_000164</Entity><Operand ID="f74c6bf3-2718-4f8c-90e1-ead1257edc3a">
                    <Name>EXCLUDE</Name>
                </Operand></Actions>
            <Conditions ID="12dd11ea-bfe7-4ef3-b35c-f06cc90b1b6b">
                <If>IF package is Private Line</If>
                <Operand ID="8d041d93-77c7-46ac-938d-753596b8535c">
                    <Name>EXISTS</Name>
                </Operand>
                <Entity>P_000137</Entity>
            </Conditions></Rule_Statement>
    </Compatibility_Rules>
</Package>
</Root>

以上是我试图转换的XML。 我需要将Compatibility_Rules显示为:

<attribute><name>Compatibility Rules</name><value GUID="b7137f22-31da-4d90-971c-b6baad654c96"></value>

上面的GUID是Package的ID属性。我不知道如何实现这一目标。

1 个答案:

答案 0 :(得分:1)

选择父元素的ID属性:

../*/@ID

使用Attribute Value Template

Compatability_Rules的{​​{1}}模板中应用于XSLT
<xsl:template match="Compatibility_Rules">
    <attribute>
            <name>
                <xsl:value-of select="local-name()"/>
            </name>
            <value GUID="{../*/@ID}"></value>
    </attribute>
</xsl:template>