从 xslt 中的数组中提取值

extract value from an array in xslt

我有变量'temperatureQualifier',其类型是数组。我需要读取该数组变量并从数组中提取每个值并在我的 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]

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

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

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

使用最新版本的 Saxon,您可以尝试 XSLT 3.0 和

                    <xsl:for-each
                        select="item/flex/attrGroupMany[@name = 'tradeItemTemperatureInformation']/row/attr[@name = 'temperatureQualifier']/json-to-xml(.)//*:number">

                        <Attribute name="code">
                            <Value>
                                <xsl:value-of select="."/>
                            </Value>
                        </Attribute>


                    </xsl:for-each>