将父节点值复制到子节点

时间:2019-08-01 13:10:30

标签: xslt

我想将parentnode元素的值复制到子节点,如下所示。有人可以为我指出正确的方向吗? 谢谢!

这是xml文件。

<mainpart id="295928" num="1-MS15" quantity="1">
    <explicitQuantity>1</explicitQuantity>
    <proxy id="1E2B4D" ACADID="1E2B4E" basepart="58A67">
        <singlepart id="24558D" num="1-m81" ncFile="1-m81.nc1" quantity="1">
            <explicitQuantity>1</explicitQuantity>
            <part id="58A67" name="C4X6.25" class="Beam" ACADID="59582" dstvName="C4X6.25">
                <explicitQuantity>1</explicitQuantity>
                <role key="Frame" name="FRAME"/>
                <length>1727.194</length>
                <paintArea>618840.532</paintArea>
                <numHoles>8</numHoles>
                <ObjectTopLevel>0.739</ObjectTopLevel>
                <ObjectBottomLevel>-101.741</ObjectBottomLevel>
                <material key="ASTM-A36" name="A36"/>
                <coating key="G" name="G"/>
                <commodity>FRAME</commodity>
                <weight>16064.68</weight>
                <exactWeight>15945.55</exactWeight>
                <weightPerMeter>9301.02</weightPerMeter>
                <density>7850.00</density>
                <section key="AISC 14.1 C Channel#@§@#ChannelsC4X6.25" name="C4X6.25"/>
                <sysLength>1727.194</sysLength>
                <sawLength>1727.194</sawLength>
                <angleX1>0.0000</angleX1>
                <angleY1>0.0000</angleY1>
                <angleX2>0.0000</angleX2>
                <angleY2>0.0000</angleY2>
                <SawCutInfo>0</SawCutInfo>
                <ElementID>7400</ElementID>
            </part>
        </singlepart>
        <singlepart id="295ADA" num="1-p3" quantity="4">
            <explicitQuantity>4</explicitQuantity>
            <part id="295C18" name="PL 1/4&quot;x1 3/8&quot;" class="Plate" ACADID="295C14" dstvName="PL 1/4&quot;">
                <explicitQuantity>1</explicitQuantity>
                <role key="Plate" name="PLATE"/>
                <length>85.725</length>
                <paintArea>7215.343</paintArea>
                <ObjectTopLevel>-7.199</ObjectTopLevel>
                <ObjectBottomLevel>-93.396</ObjectBottomLevel>
                <material key="ASTM-A36" name="A36"/>
                <coating key="G" name="G"/>
                <weight>143.81</weight>
                <exactWeight>143.81</exactWeight>
                <density>7850.00</density>
                <thickness>6.350</thickness>
                <width>35.535</width>
                <area1>2884.914</area1>
                <area2>2884.914</area2>
                <contourLength>227.640</contourLength>
                <ElementID>52306</ElementID>
                <ElementID>52304</ElementID>
                <ElementID>52303</ElementID>
                <ElementID>52305</ElementID>
            </part>
        </singlepart>
    </proxy>
</mainpart>

这是我的xsl,我知道这是错误的,但是我不知道如何解决它。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="mainpart" >
        <xsl:variable name="mainpartmark" select="@name" />
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="part">     
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
            <UserAttribute10>"$mainpartmark"</UserAttribute10>  
        </xsl:copy>     
    </xsl:template>
</xsl:stylesheet>

这是我想要的结果,我想在Part节点的每一端添加节点UserAttr10,其值来自mainpart节点。

<mainpart id="295928" num="1-MS15" quantity="1">
    <explicitQuantity>1</explicitQuantity>
    <proxy id="1E2B4D" ACADID="1E2B4E" basepart="58A67">
        <singlepart id="24558D" num="1-m81" ncFile="1-m81.nc1" quantity="1">
            <explicitQuantity>1</explicitQuantity>
            <part id="58A67" name="C4X6.25" class="Beam" ACADID="59582" dstvName="C4X6.25">
                <explicitQuantity>1</explicitQuantity>
                <role key="Frame" name="FRAME"/>
                <length>1727.194</length>
                <paintArea>618840.532</paintArea>
                <numHoles>8</numHoles>
                <ObjectTopLevel>0.739</ObjectTopLevel>
                <ObjectBottomLevel>-101.741</ObjectBottomLevel>
                <material key="ASTM-A36" name="A36"/>
                <coating key="G" name="G"/>
                <commodity>FRAME</commodity>
                <weight>16064.68</weight>
                <exactWeight>15945.55</exactWeight>
                <weightPerMeter>9301.02</weightPerMeter>
                <density>7850.00</density>
                <section key="AISC 14.1 C Channel#@§@#ChannelsC4X6.25" name="C4X6.25"/>
                <sysLength>1727.194</sysLength>
                <sawLength>1727.194</sawLength>
                <angleX1>0.0000</angleX1>
                <angleY1>0.0000</angleY1>
                <angleX2>0.0000</angleX2>
                <angleY2>0.0000</angleY2>
                <SawCutInfo>0</SawCutInfo>
                <ElementID>7400</ElementID>
                <UserAttr10>1-MS15</UserAttr10> <!--Add this line-->
            </part>
        </singlepart>
        <singlepart id="295ADA" num="1-p3" quantity="4">
            <explicitQuantity>4</explicitQuantity>
            <part id="295C18" name="PL 1/4&quot;x1 3/8&quot;" class="Plate" ACADID="295C14" dstvName="PL 1/4&quot;">
                <explicitQuantity>1</explicitQuantity>
                <role key="Plate" name="PLATE"/>
                <length>85.725</length>
                <paintArea>7215.343</paintArea>
                <ObjectTopLevel>-7.199</ObjectTopLevel>
                <ObjectBottomLevel>-93.396</ObjectBottomLevel>
                <material key="ASTM-A36" name="A36"/>
                <coating key="G" name="G"/>
                <weight>143.81</weight>
                <exactWeight>143.81</exactWeight>
                <density>7850.00</density>
                <thickness>6.350</thickness>
                <width>35.535</width>
                <area1>2884.914</area1>
                <area2>2884.914</area2>
                <contourLength>227.640</contourLength>
                <ElementID>52306</ElementID>
                <ElementID>52304</ElementID>
                <ElementID>52303</ElementID>
                <ElementID>52305</ElementID>
                <UserAttr10>1-MS15</UserAttr10> <!--Add this line-->
            </part>
        </singlepart>
    </proxy>
</mainpart>

1 个答案:

答案 0 :(得分:0)

由于只有一个mainpart节点(根元素),因此只需将变量声明为全局变量(xsl:stylesheet的子代)

<xsl:variable name="mainpartmark" select="/mainpart/@num" />

(在这种情况下,您不需要匹配mainpart的模板)。

然后,要使用它,只需执行此操作...

<UserAttribute10>
   <xsl:value-of select="/mainpart/@num" />
</UserAttribute10>  

尝试使用此XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    exclude-result-prefixes="xsi">

    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:variable name="mainpartmark" select="/mainpart/@num" />

    <xsl:template match="part">     
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
            <UserAttribute10>
                <xsl:value-of select="$mainpartmark" />
            </UserAttribute10>  
        </xsl:copy>     
    </xsl:template>
</xsl:stylesheet>

当然,您也可以完全不使用变量,而只需执行<xsl:value-of select="/mainpart/@num" />

而且,如果有多个mainpart节点(在单个根元素下),则也可以执行<xsl:value-of select="ancestor::mainpart/@num" />