XSLT-XML转换
我有以下格式的XMLXSLT-XML转换,xml,xslt,Xml,Xslt,我有以下格式的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
<?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>
]]>
我有多个这样的实体节点。我需要在每个“实体”节点中添加一个属性,作为名为“A-AssetFileName”的属性的ProductImage=值。我需要使用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>
您可以从一个将输入XML原样复制到输出的
标识模板开始
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</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>
您的思路是正确的,但似乎不太了解匹配模板的上下文节点是什么,以及如何使用与该节点相关的XPath
当模板与/x:Data/x:Entities/x:Entity
元素匹配时,该模板中的任何XPath表达式都将与该元素相对
如果存在多个x:Entity
元素,则该模板将触发多次;每个元素一次。因此,您不希望使用xs:for-each
来选择所有/x:Data/x:Entities/x:Entity
元素,并使用“跳到”XML树顶部的XPath来选择所有x:Entity
元素。这将产生一种
<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>
<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>
<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>