【问题标题】:XSLT - XML TransformationXSLT - XML 转换
【发布时间】:2018-08-23 17:57:48
【问题描述】:

我有以下格式的 XML

<?xml version="1.0" encoding="utf-8"?>
<Data Schema="RSXML4.0" xmlns="http://schemas.riversand.com/mdmcenter"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://schemas.riversand.com/mdmcenter/RSXml4.0.xsd">
    <Entities>
<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="4041" Name="SKU Dimensions" LongName="SKU Dimensions"
                    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[3/4 in x 1 in x 1/2 in]]></Value>
                    </Values>
                </Attribute>
                <Attribute Id="4042" Name="MSRP" LongName="MSRP" InstanceRefId="-1" Sequence="-1"
                    AttributeParentId="4000" AttributeParentName="Core Attribute Group"
                    AttributeType="Simple" AttributeDataType="Decimal" Locale="en_WW" Action="Add">
                    <Values>
                        <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue=""
                            HasInvalidValue="False" Locale="en_WW" Action="Add"
                            ><![CDATA[2.49]]></Value>
                    </Values>
                </Attribute>
                <Attribute Id="4045" Name="Collection" LongName="Collection" 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]]></Value>
                    </Values>
                </Attribute>
                <Attribute Id="4047" Name="UPC" LongName="UPC" 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[892030475006]]></Value>
                    </Values>
                </Attribute>
                <Attribute Id="4049" Name="Short Description" LongName="Short Description"
                    InstanceRefId="-1" Sequence="-1" AttributeParentId="4001"
                    AttributeParentName="Marketing 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 Pencil Sharpener features plastic housing with hardened and honed rust-proof carbon steel blade.]]></Value>
                    </Values>
                </Attribute>
                <Attribute Id="4050" Name="Long Description" LongName="Long Description"
                    InstanceRefId="-1" Sequence="-1" AttributeParentId="4001"
                    AttributeParentName="Marketing 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 Pencil Sharpener features plastic housing with hardened and honed rust-proof carbon steel blade. Designed for sharpening of pencils. Hand pencil sharpener comes in assorted colors and is sold as 24 per pack.]]></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"><![CDATA[]]></Value>
                                    </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"><![CDATA[]]></Value>
                                    </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>
                                <Attribute Id="4130" Name="A-AssetImageThumb"
                                    LongName="AssetImageThumb" 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[]]></Value>
                                    </Values>
                                </Attribute>
                                <Attribute Id="4131" Name="A-AssetImageType"
                                    LongName="AssetImageType" 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[]]></Value>
                                    </Values>
                                </Attribute>
                                <Attribute Id="4132" Name="A-AssetKind" LongName="AssetKind"
                                    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[]]></Value>
                                    </Values>
                                </Attribute>
                                <Attribute Id="4133" Name="A-AssetLocalURL" LongName="AssetLocalURL"
                                    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[http://smbpoc.riversand.com:8080/MAM76/assets/1/76E608495D23463EA54B253B1FB1D94C/img/991E8697FD394A17A70C0DA7A73ECE87/SSI1006_SchoolSmart_Sharpener_1.jpg]]></Value>
                                    </Values>
                                </Attribute>
                                <Attribute Id="4134" Name="A-AssetProductDescription"
                                    LongName="AssetProductDescription" 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[]]></Value>
                                    </Values>
                                </Attribute>
                                <Attribute Id="4135" Name="A-AssetRenditions"
                                    LongName="AssetRenditions" 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[<?xml version="1.0" encoding="utf-16"?><Renditions />]]></Value>
                                    </Values>
                                </Attribute>
                                <Attribute Id="4136" Name="A-AssetSize" LongName="AssetSize"
                                    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[93194]]></Value>
                                    </Values>
                                </Attribute>
                                <Attribute Id="4137" Name="A-AssetThumbnailURL"
                                    LongName="AssetThumbnailURL" 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[http://smbpoc.riversand.com:8080/MAM76/assets/1/76E608495D23463EA54B253B1FB1D94C/img/991E8697FD394A17A70C0DA7A73ECE87/thumb_991E8697FD394A17A70C0DA7A73ECE87.jpg]]></Value>
                                    </Values>
                                </Attribute>
                                <Attribute Id="4138" Name="A-AssetType" LongName="AssetType"
                                    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[img]]></Value>
                                    </Values>
                                </Attribute>
                                <Attribute Id="4139" Name="A-AssetPrimary" LongName="AssetPrimary"
                                    InstanceRefId="-1" Sequence="-1" AttributeParentId="4122"
                                    AttributeParentName="A-Assets" AttributeType="Simple"
                                    AttributeDataType="String" Locale="en_WW" Action="Add">
                                    <Values>
                                        <Value Id="-1" Uom="" ValueRefId="2" Sequence="-1"
                                            DisplayValue="Secondary" HasInvalidValue="False"
                                            Locale="en_WW" Action="Add"
                                            ><![CDATA[Secondary]]></Value>
                                    </Values>
                                </Attribute>
                                <Attribute Id="4140" Name="A-AssetSequence" LongName="AssetSequence"
                                    InstanceRefId="-1" Sequence="-1" AttributeParentId="4122"
                                    AttributeParentName="A-Assets" AttributeType="Simple"
                                    AttributeDataType="Integer" Locale="en_WW" Action="Add">
                                    <Values>
                                        <Value Id="-1" Uom="" ValueRefId="0" Sequence="-1"
                                            DisplayValue="" HasInvalidValue="False" Locale="en_WW"
                                            Action="Add"><![CDATA[1]]></Value>
                                    </Values>
                                </Attribute>
                                <Attribute Id="4142" Name="A-FilePath" LongName="AssetFilePath"
                                    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[C:\Users\RSAdmin\Documents\Templates\Print Images\SSI1006_SchoolSmart_Sharpener_1.jpg]]></Value>
                                    </Values>
                                </Attribute>
                            </Attributes>
                        </Attribute>
                    </Attributes>
                </Attribute>
                <Attribute Id="4618" Name="ItemQty" LongName="ItemQty" 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[Pack of 24]]></Value>
                    </Values>
                </Attribute>
                <Attribute Id="4619" Name="ItemColor" LongName="ItemColor" 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[Assorted]]></Value>
                    </Values>
                </Attribute>
            </Attributes>
            <Relationships/>
        </Entity>
    </Entities>
</Data>

我有多个这样的实体节点。我需要在每个“实体”节点中添加一个属性作为 ProductImage = 名称为“A-AssetFileName”的属性的值。我需要使用 XSLT 来做到这一点。任何帮助,将不胜感激。下面的 XSLT 将每个图像值的值添加到同一个实体节点。我希望每个实体节点都从名为“A-AssetFileName”的属性中获得相应的图像值。例如: 我的第一个实体节点应该变成:

<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" ProductImage="SSI1006_SchoolSmart Sharpener_1.jpg">

我正在使用下面的 XSLT。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:x="http://schemas.riversand.com/mdmcenter">
    <xsl:output omit-xml-declaration="yes" indent="yes" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/x:Data/x:Entities/x:Entity">
        <xsl:for-each select = "/x:Data/x:Entities/x:Entity"> 
        <xsl:copy>

<!--                <xsl:choose>-->
<!--                    <xsl:when test='/x:Data/x:Entities/x:Entity/x:Attributes/x:Attribute/x:Attributes/x:Attribute[@Name = "A-Assets Instance Record"]'>-->
<!--                        <xsl:if test='/x:Data/x:Entities/x:Entity/x:Attributes/x:Attribute/x:Attributes/x:Attribute[@Sequence = "0"]'>-->

                            <xsl:attribute name="ProductImage"> 
                                <xsl:for-each select = "/x:Data/x:Entities/x:Entity/x:Attributes/x:Attribute/x:Attributes/x:Attribute/x:Attributes/x:Attribute[@Name = 'A-AssetFileName']">
                                <xsl:value-of select="."/>
                                </xsl:for-each>
                            </xsl:attribute>

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

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

</xsl:stylesheet>

【问题讨论】:

  • 如果您至少提供了所需输出的示例,将会很有帮助。如果您没有要共享的 XSLT 并且不确定从哪里开始,那很好,但至少提供所需输出的清晰示例。即,第一个 Entity 元素将变为 &lt;Entity Image="abc.jpg"&gt;,并删除 &lt;value&gt; 元素中的任何前导/尾随空格。

标签: xml xslt


【解决方案1】:

您可以从 identity template 开始,将输入 XML 原样复制到输出。

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

由于您需要向Entity 元素添加属性,因此使用了匹配模板。属性Image 在模板中创建,其值对应于Entity 模板中元素Attribute/value 的值。函数normalize-space() 用于去除任何前导和尾随空格。

<xsl:template match="Entity">
    <xsl:copy>
        <xsl:attribute name="Image">
            <xsl:value-of select="normalize-space(Attribute/value)" />
        </xsl:attribute>
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>

完整的 XSLT

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

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

    <xsl:template match="Entity">
        <xsl:copy>
            <xsl:attribute name="Image">
                <xsl:value-of select="normalize-space(Attribute/value)" />
            </xsl:attribute>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出如下。由于输入 XML 具有 abc.jpg 作为两个 value 元素的值,因此两个 Entity 元素的属性值相同。

<Data>
    <Entities>
        <Entity Image="abc.jpg">
            <Attribute name="abc">
                <value> abc.jpg
                </value>
            </Attribute>
        </Entity>
        <Entity Image="abc.jpg">
            <Attribute name="def">
                <value> abc.jpg
                </value>
            </Attribute>
        </Entity>
    </Entities>
</Data>

【讨论】:

  • 感谢 Aniket 的回复。但我仍然没有达到我想要的。请检查我的问题。我已经更新了确切的 XML 和 XSLT。
【解决方案2】:

您在正确的轨道上,但您似乎不太了解匹配模板的上下文节点是什么,以及如何使用与该节点相关的 XPath。

当模板与 /x:Data/x:Entities/x:Entity 元素匹配时,该模板内的任何 XPath 表达式都将与该元素相对。

如果有多个x:Entity 元素,该模板将触发多次;每个元素一次。因此,您不希望使用选择所有/x:Data/x:Entities/x:Entity 元素的xs:for-each 和一个“跳转”到XML 树顶部的XPath 来选择所有x:Entity 元素。这将产生制作笛卡尔积的效果。

同样,在选择属性的值时,您希望使用 XPath 选择 Attribute 元素相对于上下文节点(x:Entity由模板匹配)。

如果这些 x:Attributes/x:Attribute 元素的结构可能不同(有时是同胞,有时是后代),您可以使用更通用的 XPath 从它的任何后代中进行选择,并使用更通用的 XPath 来过滤 @name.//x:Attributes/x:Attribute[@Name = 'A-AssetFileName']/x:Values/x:Value

将更改应用到您的样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:x="http://schemas.riversand.com/mdmcenter">
    <xsl:output omit-xml-declaration="yes" indent="yes" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="/x:Data/x:Entities/x:Entity">

        <xsl:copy> <!--copy the matched Entity-->
            <xsl:apply-templates select="@*"/> <!--preserve existing attributes-->

            <xsl:attribute name="ProductImage"> 
                <!--Sele the attribute value using a relative path from the matched Entity -->
                <xsl:value-of select="x:Attributes/x:Attribute/x:Attributes/x:Attribute/x:Attributes/x:Attribute[@Name = 'A-AssetFileName']/x:Values/x:Value"/>
<!--If you aren't sure what the nesting structure of the Attributes will be, 
    then just use the descendant axis to look down as many levels as it needs to find it
                <xsl:value-of select=".//x:Attributes/x:Attribute[@Name = 'A-AssetFileName']/x:Values/x:Value"/>
-->
            </xsl:attribute>

            <!--if you don't want to preserve the Attribute elements, remove the line below -->
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 2019-10-06
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多