开发者

XSLT 1.0: can't use variable in for-each, is there an other way to solve this example?

开发者 https://www.devze.com 2023-02-16 08:08 出处:网络
I have a question about the possiblities of xslt.Can following be done with xslt? input:` <invoice>

I have a question about the possiblities of xslt. Can following be done with xslt?

input:`

<invoice>
<record><field>ROWORD;123;;;</field></record>
<record><field>ROWART;shoe;10,2;20;</field></record>
<record><field>ROWORD;124;;;</field></record>
<record><field>ROWART;ball;1,5;2;</field></record>
<record><field>ROWART;car;1000;1;</field></record>
<record><field>ROWART;tractor;900;2;</field></record>
<record><field>ROWORD;125;;;</field></record>
<record><field>ROWART;computer;100;200;</field></record>
<record><field>ROWART;shoes;10,2;20;</field></record>
<record><field>ROWORD;126;;;</field></record>
<record><field>ROWART;keyboard;100;1;</field></record>
</invoice>

output should be:

<article>
<description>shoe</description>
<price>10,2</price>
<ordernumber>123</ordernumber>

<article>
<description>ball</description>
<price>1,5</price>
<ordernumber>124</or开发者_如何学Pythondernumber>

<article>
<description>car</description>
<price>1000</price>
<ordernumber>124</ordernumber>

<article>
<description>tractor</description>
<price>900</price>
<ordernumber>124</ordernumber>

<article>
<description>computer</description>
<price>100</price>
<ordernumber>125</ordernumber>

<article>
<description>shoe</description>
<price>10,2</price>
<ordernumber>125</ordernumber>

<article>
<description>keyboard</description>
<price>100</price>
<ordernumber>126</ordernumber>

I used a for-each to loop over the records. Then I split the tag field in seperate variables (with substring-before / substring-after). With the if statement I check if the tag field starts-with ROWART, but then I have a problem. I can't find a way to find the responding ordernumber.

So, i'm looking for a way to remember ROWORD.

Maybe there's something wrong with my idea of using xslt. I use xslt 1.0. The input can't be changed.

Thanks for your help Olivier


What about just using a for-each on the ROWART items, and then the preceding-sibling::…ROWORD for the required extra data?

Like so:

<xsl:for-each select="//field[starts-with(text(),'ROWART')]">
    <article>
        <description>
            <xsl:value-of select="substring-before(substring-after(text(),';'),';')"/>
        </description>
        <price>
            <xsl:value-of select="substring-before(substring-after(substring-after(text(),';'),';'),';')"/>
        </price>
        <ordernumber>
            <xsl:variable name="ROWORDfields" select="../preceding-sibling::*[starts-with(field/text(),'ROWORD')]"/>
            <xsl:value-of select="substring-before(substring-after($ROWORDfields[last()],';'),';')"/>
        </ordernumber>
    </article>
</xsl:for-each>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号