0

我确定以前有人问过这个问题,而我只是没有使用正确的标准进行搜索,或者我已经看这个太久了,看不到树木的树木。

基本上对 XML/XSL 转换来说并不陌生,这不是我角色的重要组成部分,我有一个 XSL 转换,可以将 Microsoft SSRS 报告中的 XML 转换为格式化的文本文件,而我要做的是计算行号在正确的地方强制分页:

这是我从 SSRS 输出的 XML 输出示例:

<?xml version="1.0" encoding="utf-8"?>
<Report  Name="GR_ItemSample.srdl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="GR_ItemSample.srdl">

    <Item Detail_ItemCode="6407" Detail_Description="SOLU-CORTEF INJ.SOL.250MG ACT-" Detail_UnitOfSale="AOV" Detail_Quantity="1" Detail_UnitPrice="0,00" Detail_Gross="0,00" Detail_Discount1Percentage="0,00" Detail_Discount1Amount="0,00" Detail_Discount2Percentage="0,00" Detail_Discount2Amount="0,00" Detail_Discount3Percentage="0,00" Detail_Discount3Amount="0,00" Detail_Net="0,00" Detail_Lot="TEST123" Detail_VatPercentage="0,0" Detail_MiscDesc2="*** This is a second line description test *******" valueOnlyIdentifier="1" />
    <Item Detail_ItemCode="6407" Detail_Description="SOLU-CORTEF INJ.SOL.250MG ACT-" Detail_UnitOfSale="AOV" Detail_Quantity="1" Detail_UnitPrice="0,00" Detail_Gross="0,00" Detail_Discount1Percentage="0,00" Detail_Discount1Amount="0,00" Detail_Discount2Percentage="0,00" Detail_Discount2Amount="0,00" Detail_Discount3Percentage="0,00" Detail_Discount3Amount="0,00" Detail_Net="0,00" Detail_Lot="TEST123" Detail_VatPercentage="0,0" Detail_MiscDesc2="" valueOnlyIdentifier="1" />
    <Item Detail_ItemCode="6407" Detail_Description="SOLU-CORTEF INJ.SOL.250MG ACT-" Detail_UnitOfSale="AOV" Detail_Quantity="1" Detail_UnitPrice="0,00" Detail_Gross="0,00" Detail_Discount1Percentage="0,00" Detail_Discount1Amount="0,00" Detail_Discount2Percentage="0,00" Detail_Discount2Amount="0,00" Detail_Discount3Percentage="0,00" Detail_Discount3Amount="0,00" Detail_Net="0,00" Detail_Lot="TEST123" Detail_VatPercentage="0,0" Detail_MiscDesc2="*** This is a second line description test *******" valueOnlyIdentifier="1" />
    <Item Detail_ItemCode="6407" Detail_Description="SOLU-CORTEF INJ.SOL.250MG ACT-" Detail_UnitOfSale="AOV" Detail_Quantity="1" Detail_UnitPrice="0,00" Detail_Gross="0,00" Detail_Discount1Percentage="0,00" Detail_Discount1Amount="0,00" Detail_Discount2Percentage="0,00" Detail_Discount2Amount="0,00" Detail_Discount3Percentage="0,00" Detail_Discount3Amount="0,00" Detail_Net="0,00" Detail_Lot="TEST123" Detail_VatPercentage="0,0" Detail_MiscDesc2="" valueOnlyIdentifier="1" />
    <Item Detail_ItemCode="6407" Detail_Description="SOLU-CORTEF INJ.SOL.250MG ACT-" Detail_UnitOfSale="AOV" Detail_Quantity="1" Detail_UnitPrice="0,00" Detail_Gross="0,00" Detail_Discount1Percentage="0,00" Detail_Discount1Amount="0,00" Detail_Discount2Percentage="0,00" Detail_Discount2Amount="0,00" Detail_Discount3Percentage="0,00" Detail_Discount3Amount="0,00" Detail_Net="0,00" Detail_Lot="TEST123" Detail_VatPercentage="0,0" Detail_MiscDesc2="*** This is a second line description test *******" valueOnlyIdentifier="1" />
</Report>

这是我的示例 xsl 文件:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rpt="GR_ItemSample.srdl">
<xsl:output method="text" omit-xml-declaration="yes" encoding="utf-8" media-type="text/plain"/> 
<xsl:template match="rpt:Report">
    <xsl:variable name="CrLf1" select="'&#13;&#10;'"/>
    <xsl:for-each select="rpt:Item">
        <xsl:value-of select="concat(position(),' - ',@Detail_ItemCode,@Detail_Description,@Detail_UnitOfSale,@Detail_Quantity,@Detail_UnitPrice,@Detail_Gross,@Detail_Discount1Percentage,@Detail_Discount1Amount,@Detail_Discount2Percentage,@Detail_Discount2Amount,@Detail_Discount3Percentage,@Detail_Discount3Amount,@Detail_Net,@Detail_Lot,@Detail_VatPercentage,$CrLf1)"/> 
        <xsl:if test="string-length(@Detail_MiscDesc2) > 0">
            <xsl:value-of select="concat(position(),' - ',@Detail_MiscDesc2,$CrLf1)"/>              
        </xsl:if>
    </xsl:for-each> 
</xsl:template>
</xsl:stylesheet>

现在,当我运行转换时,我得到以下输出,这与我想要的很接近:

1 - 6407SOLU-CORTEF INJ.SOL.250MG ACT-AOV10,000,000,000,000,000,000,000,000,00TEST1230,0
1 - *** This is a second line description test *******
2 - 6407SOLU-CORTEF INJ.SOL.250MG ACT-AOV10,000,000,000,000,000,000,000,000,00TEST1230,0
3 - 6407SOLU-CORTEF INJ.SOL.250MG ACT-AOV10,000,000,000,000,000,000,000,000,00TEST1230,0
3 - *** This is a second line description test *******
4 - 6407SOLU-CORTEF INJ.SOL.250MG ACT-AOV10,000,000,000,000,000,000,000,000,00TEST1230,0
5 - 6407SOLU-CORTEF INJ.SOL.250MG ACT-AOV10,000,000,000,000,000,000,000,000,00TEST1230,0
5 - *** This is a second line description test *******

我正在使用 position() 在我的 for-each 循环中显示“行号”,现在当我在文本文件中为每一行打印一行时,这是完美的,但是添加第二个描述的新要求行已被请求,第二个描述行出现在与@Detail_MiscDesc2 相同的项目行上,但它必须出现在文本文件输出中的下一行,我的xsl 根据@Detail_MiscDesc2 的字符串长度有条件地执行此操作,但是此时我需要知道累积的行号,所以我的示例输出实际上应该如下所示:

1 - 6407SOLU-CORTEF INJ.SOL.250MG ACT-AOV10,000,000,000,000,000,000,000,000,00TEST1230,0
2 - *** This is a second line description test *******
3 - 6407SOLU-CORTEF INJ.SOL.250MG ACT-AOV10,000,000,000,000,000,000,000,000,00TEST1230,0
4 - 6407SOLU-CORTEF INJ.SOL.250MG ACT-AOV10,000,000,000,000,000,000,000,000,00TEST1230,0
5 - *** This is a second line description test *******
6 - 6407SOLU-CORTEF INJ.SOL.250MG ACT-AOV10,000,000,000,000,000,000,000,000,00TEST1230,0
7 - 6407SOLU-CORTEF INJ.SOL.250MG ACT-AOV10,000,000,000,000,000,000,000,000,00TEST1230,0
8 - *** This is a second line description test *******

我需要这个,因为我还必须计算分页符和页眉和页脚,我知道对于我的文具,我每页总共只能有 22 个详细信息行。我实际上不需要打印行号(仅用于此示例)我只需要知道何时达到 22 行,以便处理页脚和分页符。我更习惯于过程式编程语言,这很容易。我知道我不能为此使用 position() 并且需要使用递归模板,但我就是不知道该怎么做。任何帮助或指示将不胜感激。我试图提供尽可能多的细节,而不会太啰嗦(是的,我知道那里失败了!)但如果有任何不清楚的地方,请告诉我。

谢谢

4

1 回答 1

0

代替打印position(),您可以打印count()前面的 Items,加上前面 Items 的计数,并带有强制第二行的描述。就像是:

count(preceding-sibling::*) + count(preceding-sibling::*[string-length(@Detail_Misc2) &gt; 0])
于 2013-07-14T16:50:31.777 回答