XSL:str的问题:拆分for-each

时间:2011-12-02 08:02:33

标签: xslt split

声明:没有数据是真实数据,所有数据都是虚拟的,包括标签,但逻辑是真实的。

XML文件

<MasterData>
<data>
    <document type="1"> <!-- Document 1 -->
        <configuration>
            <attribute_info>
                <attribute>Value similar to Document 2
                </attribute>
                <attribute>Value similar to Document 2
                </attribute>
            </attribute_info>
        </configuration>
        <Model>
            <Name>Products</Name>
            <ModelType>T1</ModelType>
        </Model>
    </document>
    <document type="2"> <!-- Document 2 -->
        <configuration>
            <attribute_info>
                <attribute name="shift" value="6$,$7$,$8$,$9$,$$,$$,$$,$" label="shift">6$,$7$,$8$,$9$,$$,$$,$$,$</attribute>
                <attribute name="serviceType" value="Replace$,$Standard Replace$,$Specific Replace$,$Bar Replace$,$Bar & Plate Replace$,$Bush Replace$,$Bush Replace - Standard$,$Bush Replace - Specific" label="serviceType">Replace$,$Standard Replace$,$Specific Replace$,$Bar Replace$,$Bar & Plate Replace$,$Bush Replace$,$Bush Replace - Standard$,$Bush Replace - Specific</attribute>
                <attribute name="hrs" value="1$,$2$,$2.5$,$4$,$1$,$2$,$6$,$6" label="hrs">1$,$2$,$2.5$,$4$,$1$,$2$,$6$,$6</attribute>
            </attribute_info>
        </configuration>
        <Model>
            <Name>Service</Name>
            <ModelType>T2</ModelType>
        </Model>
    </document>
    <document type="2"> <!-- Document 3 -->
        <configuration>
            <attribute_info>
                <attribute>Value similar to Document 2
                </attribute>
                <attribute>Value similar to Document 2
                </attribute>
            </attribute_info>
        </configuration>
        <Model>
            <Name>Service</Name>
            <ModelType>T2</ModelType>
        </Model>
    </document>
</data></MasterData>

XSL文件

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:str="http://exslt.org/strings">
<xsl:template match="/">
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
        <Worksheet ss:Name="Excel Output Doc Mockup">
            <Table ss:ExpandedColumnCount="100" ss:ExpandedRowCount="600" x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="42" ss:DefaultRowHeight="11.25">
                <xsl:for-each select="/MasterData/data/document[(normalize-space(./@type)='2') and (normalize-space(./Model/Name)='Service' and normalize-space(./Model/ModelType)='T2')]">

                    <xsl:variable name="shift" select="str:split(./configuration/attribute_info/attribute[@name='shift'],'$,$')" />
                    <xsl:variable name="hrs" select="str:split(./configuration/attribute_info/attribute[@name='hrs'],'$,$')" />

                    <xsl:for-each select="str:split(./configuration/attribute_info/attribute[@name='serviceType'],'$,$')">
                        <Row ss:AutoFitHeight="0">
                            <Cell ss:Index="1">
                                <Data ss:Type="String">
                                    <xsl:value-of select="." />
                                </Data>
                            </Cell>
                            <Cell ss:Index="2">
                                <Data ss:Type="String">
                                    <xsl:value-of select="$shift[position()]"/>
                                </Data>
                            </Cell>
                            <Cell ss:Index="3">
                                <Data ss:Type="String">
                                    <xsl:value-of select="$hrs[position()]" />
                                </Data>
                            </Cell>
                        </Row>
                    </xsl:for-each>

                </xsl:for-each>
            </table>
        </Worksheet>
    </Workbook>
</xsl:template></xsl:stylesheet>

excel工作簿/工作表中的预期结果

Column1 Column2 Column3

替换6 1

标准替换7 2

具体替换8 2.5

Bar替换9 4

Bar&amp;板替换1

布什取代2

布什取代 - 标准6

布什替换 - 具体6


但我得到的结果是

Column1 Column2 Column3

替换1 2

标准替换2.5 4

特定替换1 2

Bar替换6 6

Bar&amp;板更换

布什取代

布什取代 - 标准

布什替换 - 具体

我似乎得到了最新拆分字符串的值,即使我使用各自的变量引用相应的拆分字符串。

1 个答案:

答案 0 :(得分:2)

而不是

<xsl:value-of select="$shift[position()]"/> 

<强>不要

<xsl:value-of select="$shift[$vPos+0]"/>  

并在$vPos

之后立即定义xsl:for-each
<xsl:for-each select=
  "str:split(./configuration/attribute_info/attribute[@name='serviceType'],'$,$')">

  <xsl:variable name="vPos" select="position()"/>  

<强>记住:   position()函数是上下文相关的。

其价值在于;

<xsl:value-of select="$shift[position()]"/> 

当前节点列表中current()的位置。