xml使用xslt创建具有动态值的属性

时间:2018-08-15 10:36:01

标签: xml xslt

我有以下XML。我需要向第一个“实体”节点添加属性。该属性应具有属性节点“ A-AssetFileName”中的值。数据/实体中有多个实体节点。

<?xml version="1.0" encoding="UTF-8"?>
<Entity Id="347" ExternalId="SSI1006" LongName="School Smart Hand Held Rustproof Carbon Steel Pencil Sharpener, Assorted Color, Pack of 24" EntityTypeId="18" EntityTypeName="Parent SKU" ContainerId="2" ContainerName="Collaboration Master" OrganizationName="DL" CategoryId="360" CategoryName="Sharpners" CategoryLongName="Sharpners" CategoryPath="SchoolSupplies»School_Office_Supplies»Sharpners" CategoryLongNamePath="" ParentEntityId="360" ParentExternalId="Sharpners" ParentExtensionEntityId="0" ParentExtensionEntityExternalId="" ParentExtensionEntityContainerId="0" ParentExtensionEntityContainerName="" ParentExtensionEntityCategoryId="0" ParentExtensionEntityCategoryPath="" ParentExtensionEntityCategoryLongNamePath="" Locale="en_WW" Action="Add">
<Attributes>
    <Attribute Id="4039" Name="Parent SKU #" LongName="Product Number" InstanceRefId="-1" Sequence="-1" AttributeParentId="4000" AttributeParentName="Core Attribute Group" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
        <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add">
                <![CDATA[9-07466-030]]>
            </Value>
        </Values>
    </Attribute>
    <Attribute Id="4040" Name="Product Name" LongName="Product Name" InstanceRefId="-1" Sequence="-1" AttributeParentId="4000" AttributeParentName="Core Attribute Group" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
        <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add">
                <![CDATA[School Smart Hand Held Rustproof Carbon Steel Pencil Sharpener]]>
            </Value>
        </Values>
    </Attribute>
   <Attribute Id="4122" Name="A-Assets" LongName="Assets" InstanceRefId="-1" Sequence="-1" AttributeParentId="4003" AttributeParentName="Media Attribute Group" AttributeType="ComplexCollection" AttributeDataType="String" Locale="en_WW" Action="Add">
        <Attributes>
            <Attribute Id="4122" Name="A-Assets Instance Record" LongName="Assets Instance Record" InstanceRefId="293" Sequence="0" AttributeParentId="4003" AttributeParentName="Media Attribute Group" AttributeType="Complex" AttributeDataType="String" Locale="en_WW" Action="Add">
                <Attributes>
                    <Attribute Id="4126" Name="A-AssetDescription" LongName="AssetDescription" InstanceRefId="-1" Sequence="-1" AttributeParentId="4122" AttributeParentName="A-Assets" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
                        <Values>
                            <Value Id="-1" Uom="" ValueRefId="0" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add" />
                        </Values>
                    </Attribute>
                    <Attribute Id="4127" Name="A-AssetDisplayName" LongName="AssetDisplayName" InstanceRefId="-1" Sequence="-1" AttributeParentId="4122" AttributeParentName="A-Assets" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
                        <Values>
                            <Value Id="-1" Uom="" ValueRefId="0" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add" />
                        </Values>
                    </Attribute>
                    <Attribute Id="4128" Name="A-AssetFileName" LongName="AssetFileName" InstanceRefId="-1" Sequence="-1" AttributeParentId="4122" AttributeParentName="A-Assets" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
                        <Values>
                            <Value Id="-1" Uom="" ValueRefId="0" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add">
                                <![CDATA[SSI1006_SchoolSmart Sharpener_1.jpg]]>
                            </Value>
                        </Values>
                    </Attribute>
                    <Attribute Id="4129" Name="A-AssetID" LongName="AssetID" InstanceRefId="-1" Sequence="-1" AttributeParentId="4122" AttributeParentName="A-Assets" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
                        <Values>
                            <Value Id="-1" Uom="" ValueRefId="0" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add">
                                <![CDATA[991E8697FD394A17A70C0DA7A73ECE87]]>
                            </Value>
                        </Values>
                    </Attribute>
                </Attributes>
            </Attribute>
        </Attributes>
    </Attribute>
</Attributes>
<Relationships />

1 个答案:

答案 0 :(得分:0)

对于与<Entity>匹配的模板,需要做几处更改。

您已经提到,仅需要为第一个<xsl:template match="Entity[1]"> 节点包括该属性。在这种情况下,匹配的模板看起来像

@Name

为属性'选择值时,该值应用单引号<xsl:value-of select="//Attribute[@Name = 'A-AssetFileName']/Values/Value" /> 括起来,此外,还需要按如下所示修改XPath。

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

最后,当应用其他模板时,需要应用所有节点和属性,否则将不应用属性。

<xsl:template match="Entity[1]">
    <xsl:copy>
        <xsl:attribute name='ProductImage'> 
            <xsl:value-of select="//Attribute[@Name = 'A-AssetFileName']/Values/Value" />
        </xsl:attribute>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

所以模板看起来像

TIMESTAMP_DIFF(a.ts,b.ts, SECOND)