我有一个示例XML
<document>
<item>
<gtin>1000909090</gtin>
<flex>
<pegMeasurements>
<pegHoleNumber>2</pegHoleNumber>
<pegHorizontal uom="CMT">10</pegHorizontal>
<pegVertical uom="INH">20</pegVertical>
<pegHoleType>ES1</pegHoleType>
</pegMeasurements>
</flex>
</item>
</document>
我正在使用的 XSLT
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="document">
<CatalogItem>
<RelationshipData>
<Relationship>
<RelationType>Item_Master_TRADEITEM_TEMPERATURE_MVL</RelationType>
<RelatedItems>
<xsl:for-each select="item/flex/pegMeasurements//@uom">
<RelatedItem>
<xsl:attribute name="referenceKey">
<xsl:value-of select="concat('TradeItemTemperatureInformation_MVL','-', .,'-', ../../pegHoleNumber,'-', ../../pegHoleType ,'-',name(../),'-' )"/>
</xsl:attribute>
</RelatedItem>
</xsl:for-each>
</RelatedItems>
</Relationship>
</RelationshipData>
</CatalogItem>
</xsl:template>
</xsl:stylesheet>
我能够读取除 pegHorizontal 和 pegVertical 值之外的所有值,它们在数据末尾分别为 10 和 20。
预期输出应为
<?xml version="1.0" encoding="UTF-8"?>
<CatalogItem>
<RelationshipData>
<Relationship>
<RelationType>Item_Master_TRADEITEM_TEMPERATURE_MVL</RelationType>
<RelatedItems>
<RelatedItem referenceKey="TradeItemTemperatureInformation_MVL-CMT-2-ES1-pegHorizontal-10" />
<RelatedItem referenceKey="TradeItemTemperatureInformation_MVL-INH-2-ES1-pegVertical-20" />
</RelatedItems>
</Relationship>
有人可以指导最后附加什么代码吗?请不要更改完整的代码,因为我还有其他部分。
尝试:
<xsl:value-of select="concat('TradeItemTemperatureInformation_MVL-', ., '-', ../../pegHoleNumber, '-', ../../pegHoleType, '-', name(..), '-', ..)"/>
或者更方便一点:
...
<RelatedItems>
<xsl:for-each select="item/flex/pegMeasurements">
<xsl:variable name="num" select="pegHoleNumber" />
<xsl:variable name="typ" select="pegHoleType" />
<xsl:for-each select="*[@uom]">
<RelatedItem>
<xsl:attribute name="referenceKey">
<xsl:text>TradeItemTemperatureInformation_MVL-</xsl:text>
<xsl:value-of select="@uom"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="$num"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="$typ"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="."/>
</xsl:attribute>
</RelatedItem>
</xsl:for-each>
</xsl:for-each>
</RelatedItems>
...
给定输入:
<document>
<item>
<gtin>1000909090</gtin>
<flex>
<pegMeasurements>
<pegHoleNumber>2</pegHoleNumber>
<pegHorizontal uom="CMT">10</pegHorizontal>
<pegVertical uom="INH">20</pegVertical>
<pegHoleType>ES1</pegHoleType>
</pegMeasurements>
</flex>
</item>
</document>
并给定 XSLT 样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="document">
<CatalogItem>
<RelationshipData>
<Relationship>
<RelationType>Item_Master_TRADEITEM_TEMPERATURE_MVL</RelationType>
<RelatedItems>
<xsl:for-each select="item/flex/pegMeasurements//@uom">
<RelatedItem>
<xsl:attribute name="referenceKey">
<xsl:variable name="peg-hole-number" select="../../pegHoleNumber/text()"/>
<xsl:variable name="peg-name" select="name(parent::*)"/>
<xsl:variable name="peg-uom-value" select="parent::*/@uom"/>
<xsl:variable name="peg-value" select="parent::*/text()"/>
<xsl:variable name="peg-hole-type" select="../../pegHoleType/text()"/>
<xsl:value-of select="concat('TradeItemTemperatureInformation_MVL-', $peg-uom-value, '-', $peg-hole-number, '-', $peg-hole-type, '-', $peg-name, '-', $peg-value)"/>
</xsl:attribute>
</RelatedItem>
</xsl:for-each>
</RelatedItems>
</Relationship>
</RelationshipData>
</CatalogItem>
产量输出:
<?xml version="1.0" encoding="UTF-8"?>
<CatalogItem>
<RelationshipData>
<Relationship>
<RelationType>Item_Master_TRADEITEM_TEMPERATURE_MVL</RelationType>
<RelatedItems>
<RelatedItem referenceKey="TradeItemTemperatureInformation_MVL-CMT-2-ES1-pegHorizontal-10"/>
<RelatedItem referenceKey="TradeItemTemperatureInformation_MVL-INH-2-ES1-pegVertical-20"/>
</RelatedItems>
</Relationship>
</RelationshipData>
编辑:没有使用模板,因为 OP 不希望代码发生太大变化。