纯文本文档的右对齐位置编号(XSLT)

时间:2015-09-25 12:54:50

标签: xml xslt

我是XSLT和XPath的新手。 XML就是这样:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>

<catalog>
    <cd>
        <title>Empire</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
</catalog>

这是指定前一个转换规则的XSL文件:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:variable name="nl" select="'&#10;'"/>
    <xsl:variable name="cd_catalog" select="catalog/cd"/>
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:for-each select="$cd_catalog">
            <xsl:value-of select="concat(position(),'&#32;',title,$nl)"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

这是输出:

1 Empire
2 Hide your heart
3 Hide your heart
4 Hide your heart
5 Hide your heart
6 Hide your heart
7 Hide your heart
8 Hide your heart
9 Hide your heart
10 Hide your heart
11 Hide your heart
12 Hide your heart
13 Hide your heart

我想要的与我得到的几乎完全一样,差别很小。我希望每个标题前的数字右对齐三个空格,如下所示:

  1 Empire
  2 Hide your heart
  3 Hide your heart
...................
 10 Hide your heart
 11 Hide your heart
...................
100 Some title here
101 Some title here

当然它需要更长的CD列表,这不是问题。

1 个答案:

答案 0 :(得分:1)

以这种方式尝试:

XSLT 1.0

function BarChart(id, data, ticksl) {
    $.plot(id, data, {
        series: {
            //  stack: true
        },
        grid: {
            hoverable: true,
            clickable: true
            //,horizontal: true
        },
        bars: {
            show: true,
            barWidth: 0.5,
            align: "center"
        }
        , xaxis: { ticks: ticksl },
        legend: {
            show: true
        }
    });
}