[英]how to get the tag value in XSLT
我有一個樣本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不想代碼更改太多,所以不使用模板。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.