使用子数据转换XML

时间:2017-03-22 00:54:29

标签: xml xslt-1.0

我有以下数据要转换,但我的转换技巧不是很好。

<anchorSet key="1">
    <explodeAble>1</explodeAble>
    <set key="MuS" name="NaW"/>
    <standard key="HOLDING DOWN BOLTS"/>
    <diameter>24.000</diameter>
    <material key="8.8" name="8.8"/>
    <item class="Anchor" diameter="24.000">
        <material key="8.8" name="8.8"/>
        <standard key="HOLDING DOWN BOLTS" name="HOLDING DOWN BOLTS"/>
        <denotation>M24 x 450 HD BOLT</denotation>
        <weight>1700.00</weight></item>
    <item class="Washer" diameter="24.000">
        <material key="S355JR" name="S355JR"/>
        <standard key="Flat anchor" name="Flat anchor"/>
        <denotation>Flat anchor M24 - 100 x 100 x 6</denotation>
        <weight>450.00</weight></item>
    <item class="Washer" diameter="24.000">
        <material key="8.8" name="8.8"/>
        <standard key="Flat" name="Flat"/>
        <denotation>M24 Plain Washer - 8.8</denotation>
        <weight>32.00</weight></item>
    <item class="Nut" diameter="24.000">
        <material key="GR8" name="GR8"/>
        <standard key="HEX" name="HEX"/>
        <denotation>M24 Hexagonal Nut - GR8</denotation>
        <weight>104.00</weight></item>
</anchorSet>

我想将数据从Denotation子元素复制到Item Class="anchor"区域中存在的标准密钥节点。

例如,从上面会变成:

<anchorSet key="1">
    <explodeAble>1</explodeAble>
    <set key="MuS" name="NaW"/>
    <standard key="M24 x 450 HD BOLT"/>   <<--This has been modifed
    <diameter>24.000</diameter>
    <material key="8.8" name="8.8"/>
    <item class="Anchor" diameter="24.000">
        <material key="8.8" name="8.8"/>
        <standard key="HOLDING DOWN BOLTS" name="HOLDING DOWN BOLTS"/>
        <denotation>M24 x 450 HD BOLT</denotation>   <<--By this Data from Here
        <weight>1700.00</weight></item>
    <item class="Washer" diameter="24.000">
        <material key="S355JR" name="S355JR"/>
        <standard key="Flat anchor" name="Flat anchor"/>
        <denotation>Flat anchor M24 - 100 x 100 x 6</denotation>
        <weight>450.00</weight></item>
    <item class="Washer" diameter="24.000">
        <material key="8.8" name="8.8"/>
        <standard key="Flat" name="Flat"/>
        <denotation>M24 Plain Washer - 8.8</denotation>
        <weight>32.00</weight></item>
    <item class="Nut" diameter="24.000">
        <material key="GR8" name="GR8"/>
        <standard key="HEX" name="HEX"/>
        <denotation>M24 Hexagonal Nut - GR8</denotation>
        <weight>104.00</weight></item>
</anchorSet>

我很感激这个帮助。

1 个答案:

答案 0 :(得分:1)

如果看起来只想修改key元素的set属性,该属性是根anchorSet元素的子元素,并保留其他所有内容,然后做:

XSLT 1.0

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

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

<xsl:template match="/anchorSet/standard/@key">
    <xsl:attribute name="key">
        <xsl:value-of select="../../item[@class='Anchor']/denotation"/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>