从 xslt 中的数组中提取值 [英] extract value from an array in xslt

查看:29
本文介绍了从 xslt 中的数组中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量温度限定符",它的类型是数组.我需要读取该数组变量并从数组中提取每个值并在我的 XSLT 中使用它.

I have variable 'temperatureQualifier' whose type is array. I need to read that array variable and extract each value from the array and use it in my XSLT.

示例输入 XML 是

<document>
<item>
    <gtin>1000909090</gtin>
    <flex>
        <attrGroupMany name="tradeItemTemperatureInformation">
            <row>
                <attr name="temperatureQualifier">[10, 20, 30, 40]</attr>
            </row>

        </attrGroupMany>
    </flex>
</item>
</document>

所需的输出 XML 应该是

<?xml version="1.0" encoding="UTF-8"?>
<CatalogItem>
<RelationshipData>
  <Relationship>
     <RelationType>Item_Master_TRADEITEM_TEMPERATURE_MVL</RelationType>
     <RelatedItems>
        <Attribute name="code">
           <Value>10</Value>
        </Attribute>
         <Attribute name="code">
           <Value>20</Value>
        </Attribute>
         <Attribute name="code">
           <Value>30</Value>
        </Attribute>
         <Attribute name="code">
           <Value>40</Value>
        </Attribute>
     </RelatedItems>
  </Relationship>
</RelationshipData>
</CatalogItem>

我正在使用下面的 XSLT,但它只给我 1 个节点中的所有值.

<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/attrGroupMany[@name ='tradeItemTemperatureInformation']/row">                       

                        <Attribute name="code">
                            <Value>
                                <xsl:value-of select="attr[@name='temperatureQualifier']"/>
                            </Value>
                        </Attribute>


                    </xsl:for-each>
                </RelatedItems>
            </Relationship>
        </RelationshipData>
    </CatalogItem>

</xsl:template> 

</xsl:stylesheet>

注意:数组中值的个数可以是 1 或大于 1.单值数组的例子是 [10]

Note: Number of value in array can be 1 or more than 1. Example for single value array is [10]

多值数组的例子是 [10, 20, 30, 40]

Example for multie value array is [10, 20, 30, 40]

推荐答案

使用 XST 1.0,您可以使用递归拆分:

With XST 1.0 you can use a recursive split:

<xsl:template name="split">
 <xsl:param name="str" select="."/>
  <xsl:choose>
    <xsl:when test="contains($str, ',')">
       <Attribute name="code">
       <Value>
        <xsl:value-of select="normalize-space(substring-before($str, ','))"/>
      </Value>
      </Attribute>
      <xsl:call-template name="split">
        <xsl:with-param name="str" select="substring-after($str, ',')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <Attribute name="code">
         <Value>
          <xsl:value-of select="$str"/>
        </Value>
      </Attribute>
    </xsl:otherwise>
  </xsl:choose>

</xsl:template>

并称之为:

          <xsl:call-template name="split">
            <xsl:with-param name="str" select="substring-before(
                                               substring-after(
                                               attr[@name='temperatureQualifier'], '[' )
                                               ,']' )"/>
          </xsl:call-template>

这篇关于从 xslt 中的数组中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆