xsl - 在元素下标中使用数学? [英] xsl -- using math in element subscripts?

查看:64
本文介绍了xsl - 在元素下标中使用数学?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个XSL转换,将关于车辆的XML数据转换为将填充打印表单的XML数据。


默认表单可以处理使用

下标处理的5辆车...


< xsl:for-each select =" VEHICLE [1]">

<! - 吐出一些关于这辆车的东西 - >

< / xsl:for-each>

< ; xsl:for-each select =" VEHICLE [2]">

<! - 吐出一些关于这辆车的东西 - >

< / xsl:for-each>

< xsl:for-each select =" VEHICLE [3]">

<! - 吐关于这辆车的一些东西 - >

< / xsl:for-each>

< xsl:for-each select =" VEHICLE [4] ">

<! - 吐出一些关于这辆车的东西 - >

< / xsl:for-each>

< xsl:for-each select =" VEHICLE [5]">

<! - 吐出一些关于此车辆的东西e - >

< / xsl:for-each>


然后,如果有超过5辆车,我需要输出它们, 5在

时间,在一个或多个分机上根据需要形成。请注意,

额外的车辆并不总是5的倍数。


我不知道如何在通用中处理这个问题方式没有

硬编码条件形式,每5的最多可达一些

任意最大值。


我在想为了一般地处理它,我需要做一些事情

棘手的数学在下标中可能使用mod操作符但是

我不确定是否'甚至可能。


有没有人处理类似的问题?


如果我的描述不是,我可以发布更详细的例子明确。只需

问。

解决方案

William Krick写道:


我正在编写一个XSL转换,将关于车辆的XML数据转换成将填充打印表格的XML数据。


默认表格我可以使用

下标来处理多达5辆车...


< xsl:for-each select =" VEHICLE [1]" ;>

<! - 吐出一些关于这辆车的东西 - >

< / xsl:for-each>



....


是的,复制和粘贴不是最佳解决方案......
< blockquote class =post_quotes>
然后,如果有超过5辆车,我需要在

时间输出它们到一个或多个分机上。根据需要形成。请注意,

额外的车辆并不总是5的倍数。


我不知道如何在通用中处理这个问题方式没有

硬编码条件形式,每5的最多可达一些

任意最大值。


我在想为了一般地处理它,我需要做一些事情

棘手的数学在下标中可能使用mod操作符但是

我不确定是否'甚至可能。



你是对的。


我愚弄了一下。您可以使用此(我希望)作为模板:


< xsl:stylesheet xmlns:xsl =" http://www.w3.org/1999/XSL/Transform" ;

version =" 1.0">


< xsl:template match =" cars">

< report>

< xsl:apply-templates select =" car [(position() - 1)mod 5 = 0]" />

< / report>

< / xsl:template>


< xsl:template match =" car [1]">

< first-page>

< xsl:apply-templates select ="。 | follow-sibling :: car [position()

& lt; 5]" mode =" info" />

< / first-page>

< / xsl:template>


< xsl:template match =" car [(position() - 1)!= 0]">

< extra-page>

< ; xsl:apply-templates select ="。 | follow-sibling :: car [position()

& lt; 5]" mode =" info" />

< / extra-page>

< / xsl:template>


< xsl:template match =" car" mode =" info">

< carinfo car =" {@ id}" />

< / xsl:template>


< / xsl:stylesheet>

输入


< cars>

< car id =" 1" />

< car id =" 2" />

< car id =" 3" />

< car id =" 4" />

< car id =" 5" />

< car id =" 6" />

< car id =" 7" />

< car id =" 8" />

< car id =" 9" />

< car id =" 10" />

< car id =" 11" />

< / cars>


你得到(格式化后)


[dongfang @ skodspand tmp]


xsltproc car.xsl car.xml | xmllint --format -

<?xml version =" 1.0"?>

< report>

< first -page>

< carinfo car =" 1" />

< carinfo car =" 2" />

< carinfo car =" 3" />

< carinfo car =" 4" />

< carinfo car =" 5" />

< / first-page>

< extra-page>

< carinfo car =" 6" / >

< carinfo car =" 7" />

< carinfo car =" 8" />

< carinfo car =" 9" />

< carinfo car =" 10" />

< / extra-page>

< extra-page>

< carinfo car =" 11" />

< / extra-page>

< / report>

希望有所帮助;)


(你可以自己试验看看它是如何工作的。这只是为了

让你入门)


S?ren

Soren Kuula写道:


William Krick写道:


我是编写一个XSL转换,将关于车辆的XML数据转换为将填充打印表单的XML数据。



我愚弄了一下。您可以使用此(我希望)作为模板:


< xsl:stylesheet xmlns:xsl =" http://www.w3.org/1999/XSL/Transform" ;

version =" 1.0">


< xsl:template match =" cars">

< report>

< xsl:apply-templates select =" car [(position() - 1)mod 5 = 0]" />

< / report>

< / xsl:template>


< xsl:template match =" car [1]">

< first-page>

< xsl:apply-templates select ="。 | follow-sibling :: car [position()

& lt; 5]" mode =" info" />

< / first-page>

< / xsl:template>


< xsl:template match =" car [(position() - 1)!= 0]">

< extra-page>

< ; xsl:apply-templates select ="。 | follow-sibling :: car [position()

& lt; 5]" mode =" info" />

< / extra-page>

< / xsl:template>


< xsl:template match =" car" mode =" info">

< carinfo car =" {@ id}" />

< / xsl:template>


< / xsl:stylesheet>



好​​吧,它确实有效,但我不确定我是怎么理解的。


我不喜欢理解语法。


< xsl:apply-templates select =" car [(position() - 1)mod 5 = 0]" />


"(position() - 1)mod 5 = 0"评估为下标?

似乎它会评估为真/假。


< xsl:apply-templates select ="。 | follow-sibling :: car [position()& lt;

5]" mode =" info" />


然后我不明白什么是。 |"手段。我认为这意味着:

当前节点布尔值或跟随它的东西。


无论如何,* *似乎*起作用。现在唯一的问题是

每个页面中的第一辆车。必须稍微区别对待

,因为那些创建了原始XML规范的蠢货我正试图与b
见面。在每页上,有年字样。和年龄组

车辆的领域。请注意,第一辆车上年龄组的名称

不是AGE GROUP#001。正如你所料。它只是AGE GROUP。

这里有一些示例XML输出非常接近输出我是
生成(有一个为简洁省略了很多东西)。这个例子有

12辆汽车...


< FORM>

< FIRST-PAGE>

< FIELD NAME =" YEAR#001"> 1991< / FIELD>

< FIELD NAME =" AGE GROUP"> 5< / FIELD>
< FIELD NAME =" YEAR#002"> 2007< / FIELD>

< FIELD NAME =" AGE GROUP#002"> 3< / FIELD>

< FIELD NAME =" YEAR#003"> 1997< / FIELD>

< FIELD NAME =" AGE GROUP#003"> 2< / FIELD>

< FIELD NAME =" YEAR#004"> 2001< / FIELD>

< FIELD NAME =" AGE GROUP#004"> ; 8< / FIELD>

< FIELD NAME ="年#005"> 2003< / FIELD>

< FIELD NAME =" AGE GROUP# 005"> 6< / FIELD>

< / FIRST-PAGE>

< EXTRA-PAGE>

< FIELD NAME =" COVERED AUTO#01A"> 6< / FIELD>

< FIELD NAME =" YEAR#001"> 1992< / FIELD>

< FIELD NAME =" AGE GROUP"> 5< / FIELD>

< FIELD NAME =" COVERED AUTO#02A"> 7< / FIELD>

< FIELD NAME =" YEAR#002"> 2008< / FIELD>

< FIELD NAME =" AGE GROUP#002" ;> 3< / FIELD>

< FIELD NAME =" COVERED AUTO#03A"> 8< / FIELD>

< FIELD NAME ="年#003"> 1996< / FIELD>

< FIELD NAME =" AGE GROUP#003"> 2< / FIELD>

< FIELD NAME =" COVERED AUTO#04A"> 9< / FIELD>

< FIELD NAME =" YEAR#004"> 2002< / FIELD>

< ; FIELD NAME =" AGE GROUP#004"> 8< / FIELD>

< FIELD NAME =" COVERED AUTO#05A"> 10< / FIELD>

< FIELD NAME =" YEAR#005"> 2004< / FIELD>

< FIELD NAME =" AGE GROUP#005"> 6< / FIELD>

< / EXTRA-PAGE>

< EXTRA-P AGE>

< FIELD NAME =" COVERED AUTO#01A"> 11< / FIELD>

< FIELD NAME =" YEAR#001"> 1993< / FIELD>

< FIELD NAME =" AGE GROUP"> 5< / FIELD>

< FIELD NAME =" COVERED AUTO#02A" > 12< / FIELD>

< FIELD NAME =" YEAR#002"> 1999< / FIELD>

< FIELD NAME =" AGE GROUP #002"> 3< / FIELD>

< / EXTRA-PAGE>

< / FORM>


.... AGE GROUP字段是唯一不同的字段。所有其余的

在他们的名字中都有正确的数字。请注意,该数字是页面上车辆的

位置,而不是它在原始

XML文档中的位置。


I am writing an XSL transform that converts XML data about vehicles
into XML data that will fill printed forms.

The default form can handle up to 5 vehicles which I handle using
subscripts...

<xsl:for-each select="VEHICLE[1]">
<!-- spit out some stuff about this vehicle -->
</xsl:for-each>
<xsl:for-each select="VEHICLE[2]">
<!-- spit out some stuff about this vehicle -->
</xsl:for-each>
<xsl:for-each select="VEHICLE[3]">
<!-- spit out some stuff about this vehicle -->
</xsl:for-each>
<xsl:for-each select="VEHICLE[4]">
<!-- spit out some stuff about this vehicle -->
</xsl:for-each>
<xsl:for-each select="VEHICLE[5]">
<!-- spit out some stuff about this vehicle -->
</xsl:for-each>

Then, if there are more than 5 vehicles, I need to output them, 5 at a
time, onto one or more "extension" forms as needed. Note that the
extra vehicles will not always be in multiples of 5.

I''m at a loss as to how to handle this in a generic way without
hardcoding conditional forms for every multiple of 5 up to some
arbitrary maximum.

I''m thinking that to handle it generically, I''d need to do something
tricky with math in the subscripts possibly using the mod operator but
I''m not sure if that''s even possible.

Has anyone dealt with a similar problem?

I can post more detailed examples if my description isn''t clear. Just
ask.

解决方案

William Krick wrote:

I am writing an XSL transform that converts XML data about vehicles
into XML data that will fill printed forms.

The default form can handle up to 5 vehicles which I handle using
subscripts...

<xsl:for-each select="VEHICLE[1]">
<!-- spit out some stuff about this vehicle -->
</xsl:for-each>

....

Yes, copy and paste is not the best solution...

Then, if there are more than 5 vehicles, I need to output them, 5 at a
time, onto one or more "extension" forms as needed. Note that the
extra vehicles will not always be in multiples of 5.

I''m at a loss as to how to handle this in a generic way without
hardcoding conditional forms for every multiple of 5 up to some
arbitrary maximum.

I''m thinking that to handle it generically, I''d need to do something
tricky with math in the subscripts possibly using the mod operator but
I''m not sure if that''s even possible.

You are right.

I fooled around with it a little. You can use this (I hope) as a template:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="cars">
<report>
<xsl:apply-templates select="car[(position()-1) mod 5 = 0]"/>
</report>
</xsl:template>

<xsl:template match="car[1]">
<first-page>
<xsl:apply-templates select=". | following-sibling::car[position()
&lt; 5]" mode="info"/>
</first-page>
</xsl:template>

<xsl:template match="car[(position()-1)!=0]">
<extra-page>
<xsl:apply-templates select=". | following-sibling::car[position()
&lt; 5]" mode="info"/>
</extra-page>
</xsl:template>

<xsl:template match="car" mode="info">
<carinfo car="{@id}"/>
</xsl:template>

</xsl:stylesheet>
With the input

<cars>
<car id="1"/>
<car id="2"/>
<car id="3"/>
<car id="4"/>
<car id="5"/>
<car id="6"/>
<car id="7"/>
<car id="8"/>
<car id="9"/>
<car id="10"/>
<car id="11"/>
</cars>

you get (after formatting)

[dongfang@skodspand tmp]


xsltproc car.xsl car.xml | xmllint --format -
<?xml version="1.0"?>
<report>
<first-page>
<carinfo car="1"/>
<carinfo car="2"/>
<carinfo car="3"/>
<carinfo car="4"/>
<carinfo car="5"/>
</first-page>
<extra-page>
<carinfo car="6"/>
<carinfo car="7"/>
<carinfo car="8"/>
<carinfo car="9"/>
<carinfo car="10"/>
</extra-page>
<extra-page>
<carinfo car="11"/>
</extra-page>
</report>
Hope that helped ;)

(you can experiment it yourself to see how it works. This was just to
get you started)

S?ren


Soren Kuula wrote:

William Krick wrote:

I am writing an XSL transform that converts XML data about vehicles
into XML data that will fill printed forms.


I fooled around with it a little. You can use this (I hope) as a template:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="cars">
<report>
<xsl:apply-templates select="car[(position()-1) mod 5 = 0]"/>
</report>
</xsl:template>

<xsl:template match="car[1]">
<first-page>
<xsl:apply-templates select=". | following-sibling::car[position()
&lt; 5]" mode="info"/>
</first-page>
</xsl:template>

<xsl:template match="car[(position()-1)!=0]">
<extra-page>
<xsl:apply-templates select=". | following-sibling::car[position()
&lt; 5]" mode="info"/>
</extra-page>
</xsl:template>

<xsl:template match="car" mode="info">
<carinfo car="{@id}"/>
</xsl:template>

</xsl:stylesheet>

Well, it does seem to work but I''m not sure I understand how.

I don''t understand the syntax.

<xsl:apply-templates select="car[(position()-1) mod 5 = 0]"/>

How does "(position()-1) mod 5 = 0" evaluate to a subscript?
It seems like it would evaluate to true/false.

<xsl:apply-templates select=". | following-sibling::car[position() &lt;
5]" mode="info"/>

Then I don''t understand what ". |" means. I assume it means: "the
current node boolean OR the thing that follows it".

Regardless, it *does* seem to work. The only issue now is that the
first vehicle in each "page" has to be treated slightly different
because of the morons who created the original XML spec I''m trying to
meet. On each page, there are "year" and "age group" fields for the
vehicles. Notice that the name of the age group on the first vehicle
isn''t "AGE GROUP #001" as you''d expect. It''s simply "AGE GROUP".
Here''s some sample XML output that is very close to the output I''m
generating (with a lot of stuff omitted for brevity). This example has
12 cars...

<FORM>
<FIRST-PAGE>
<FIELD NAME="YEAR #001">1991</FIELD>
<FIELD NAME="AGE GROUP">5</FIELD>
<FIELD NAME="YEAR #002">2007</FIELD>
<FIELD NAME="AGE GROUP #002">3</FIELD>
<FIELD NAME="YEAR #003">1997</FIELD>
<FIELD NAME="AGE GROUP #003">2</FIELD>
<FIELD NAME="YEAR #004">2001</FIELD>
<FIELD NAME="AGE GROUP #004">8</FIELD>
<FIELD NAME="YEAR #005">2003</FIELD>
<FIELD NAME="AGE GROUP #005">6</FIELD>
</FIRST-PAGE>
<EXTRA-PAGE>
<FIELD NAME="COVERED AUTO #01A">6</FIELD>
<FIELD NAME="YEAR #001">1992</FIELD>
<FIELD NAME="AGE GROUP">5</FIELD>
<FIELD NAME="COVERED AUTO #02A">7</FIELD>
<FIELD NAME="YEAR #002">2008</FIELD>
<FIELD NAME="AGE GROUP #002">3</FIELD>
<FIELD NAME="COVERED AUTO #03A">8</FIELD>
<FIELD NAME="YEAR #003">1996</FIELD>
<FIELD NAME="AGE GROUP #003">2</FIELD>
<FIELD NAME="COVERED AUTO #04A">9</FIELD>
<FIELD NAME="YEAR #004">2002</FIELD>
<FIELD NAME="AGE GROUP #004">8</FIELD>
<FIELD NAME="COVERED AUTO #05A">10</FIELD>
<FIELD NAME="YEAR #005">2004</FIELD>
<FIELD NAME="AGE GROUP #005">6</FIELD>
</EXTRA-PAGE>
<EXTRA-PAGE>
<FIELD NAME="COVERED AUTO #01A">11</FIELD>
<FIELD NAME="YEAR #001">1993</FIELD>
<FIELD NAME="AGE GROUP">5</FIELD>
<FIELD NAME="COVERED AUTO #02A">12</FIELD>
<FIELD NAME="YEAR #002">1999</FIELD>
<FIELD NAME="AGE GROUP #002">3</FIELD>
</EXTRA-PAGE>
</FORM>

....the AGE GROUP field is the only one that is different. All the rest
have the correct numbers in their name. Note that the number is the
position of the vehicle on the page, not it''s position in the original
XML document.


这篇关于xsl - 在元素下标中使用数学?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆