Как отправить массив XSLT в шаблон - PullRequest
0 голосов
/ 11 апреля 2019

Мне нужно взять массив данных из одного узла в XML, а затем использовать в шаблоне XSL.Входные данные выглядят так:

<InpData>
 <period number="1">
   <Storage>
     <Item weight="10.5" height="5" width="15" length="20"/>
     <Item weight="20.75" height="4.5" width="7.3" length="18"/>
     <Item weight="10.5" height="5" width="15" length="20"/>
   </Storage>
   <Transportation>
     <Items>
        <Item>
          <DestRegion value="5"/>
          <Sender name="Smith" company="BlueSky" />
          <Date day="03" month="03" year="2017" />  
        <Item/>
        <Item>
          <DestRegion value="6"/>
          <Sender name="Pith" company="BlueSky" />
          <Date day="03" month="03" year="2017" />  
        <Item/>
        <Item>
          <DestRegion value="5"/>
          <Sender name="Bill" company="BlueSky" />
          <Date day="03" month="03" year="2017" />  
        <Item/>
     <Items/>
   </Transportation>
 <period/>
 <period number="2">
 <period/>
</InpData>

И мне нужно объединить узлы и получить вывод xml вот так:

<period number="1">
<Items>
   <Item weight="10.5" senderName="Smith"/>
   <Item weight="20.75" senderName="Pith"/>
   <Item weight="10.5" senderName="Bill"/>
<Items/>
<period/>

Я сделал XSLT-скрипт:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="period">
  <xsl:variable name="weight" select="Storage/Item/@weight"></xsl:variable>

   <period number="{@number}">
    <Items>
   <xsl:apply-templates select="Transportation/Items/Item"> 
    <xsl:with-param name="weight_1">
      <xsl:value-of select="$weight" />
    </xsl:with-param>
    </xsl:apply-templates>
    </Items>
   </period>
</xsl:template>

   <xsl:template match="Transportation/Items/Item">
   <xsl:param name="weight_1"/>
   <xsl:variable name="pos" select="position()"></xsl:variable>
    <Item weight="{$weight_1[$pos]}" senderName="{Sender/@name}">
  </xsl:template>

</xsl:transform>

Но это не работает должным образом.Я могу отправить в шаблон, например, и это будет работать, если я буду использовать только $ weight_1 без индексов в шаблоне.Но я не могу использовать $ weight_1 [$ pos] в шаблоне.

Скажите, пожалуйста, как правильно использовать вес массива в XSLT?

1 Ответ

0 голосов
/ 11 апреля 2019

Почему вы не можете сделать это просто:

<xsl:template match="period">
    <period number="{@number}">
        <Items>
            <xsl:for-each select="Storage/Item">
                <xsl:variable name="i" select="position()" />
                <Item weight="{@weight}" senderName="{../../Transportation/Items/Item[$i]/Sender/@name}"/>
            </xsl:for-each>
        </Items>
    </period>
</xsl:template>

(при условии, что единственное, что связывает элементы, это их положение в родительском элементе).


Добавлено:

Чтобы сделать это так, как вы начали, вам нужно сделать что-то вроде:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/InpData">
    <root>
        <xsl:apply-templates select="period"/> 
    </root> 
</xsl:template>

<xsl:template match="period">
    <period number="{@number}">
        <Items>
            <xsl:apply-templates select="Transportation/Items/Item"> 
                <xsl:with-param name="weights" select="Storage/Item/@weight"/>
            </xsl:apply-templates>
        </Items>
    </period>
</xsl:template>

<xsl:template match="Item">
    <xsl:param name="weights"/>
    <xsl:variable name="i" select="position()" />
    <Item weight="{$weights[$i]}" senderName="{Sender/@name}"/>
</xsl:template>

</xsl:stylesheet>
...