如何在XSLT中获取标记值

时间:2016-03-02 09:41:22

标签: xml xslt xml-parsing xslt-1.0 xslt-2.0

我有一个示例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>

我能够读取除pegHorizo​​ntal和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>

有人可以指导最后附加的代码。请不要更改完整的代码,因为我还有其他部分。

2 个答案:

答案 0 :(得分:1)

尝试:

<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>
...

答案 1 :(得分:1)

给定输入:

<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并不希望代码改变太多。