470,848 Members | 949 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

XSLT loop.

Hi all,

I am try to split the some data out of a nested xml file and get all the
data in one new xml file. I have two XSLT files one takes the personal
information and one takes the data out of the fund section , then I import
each xml files into the datebase and do I relationship between the two.
This is not what my boss wants as he wants one xml file

This is my current XML

<message>
<personal>
<forenames>John</forenames>
<surname>Smith</surname>
<policynumber>123456</policynumber>
<person>
<policydetails>
<fund>
<code>ABC</code>
<units>123</units>
</fund>
<fund>
<code>xyz</code>
<units>987</units>
</fund>
</policydetails>
</message>

and I am trying to acheive this

<message>
<personal>
<forenames>John</forenames>
<surname>Smith</surname>
<policynumber>123456</policynumber>
<person>
<policydetails>
<fund>
<code>ABC</code>
<units>123</units>
</fund>
</policydetails>
<personal>
<forenames>John</forenames>
<surname>Smith</surname>
<policynumber>123456</policynumber>
<person>
<policydetails>
<fund>
<code>xyz</code>
<units>987</units>
</fund>
</policydetails>
</message>

I have been trying with <xsl:for-each> looping through personal and
policydetails but am getting some strange results!! Can somebody please
point me in the right direction.

TIA
Apr 5 '06 #1
4 2047
It's hard to offer advice without knowing details of what your
stylesheet looks like. Can you come up with a minimal example that
demonstrates the problem?
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Apr 5 '06 #2
Here is the stylesheet.

<xsl:for-each select="message/fund">

<xsl:for-each select="message">

<xsl:element name="details">
<xsl:element name="forenames"><xsl:value-of
select="//forenames"></xsl:value-of>
<xsl:element name="forenames"><xsl:value-of
select="//surnames"></xsl:value-of>
<xsl:element name="forenames"><xsl:value-of
select="//policynumber"></xsl:value-of>

<xsl:element name="forenames"><xsl:value-of
select="//fund"></xsl:value-of>
<xsl:element name="forenames"><xsl:value-of
select="//code"></xsl:value-of>
<xsl:element name="forenames"><xsl:value-of
select="//units"></xsl:value-of>
</xsl:element>

</xsl:for-each>

</xsl:for-each>

"Joe Kesselman" <ke************@comcast.net> wrote in message
news:n9******************************@comcast.com. ..
It's hard to offer advice without knowing details of what your stylesheet
looks like. Can you come up with a minimal example that demonstrates the
problem?

Apr 5 '06 #3
Iain wrote:
I have been trying with <xsl:for-each> looping through personal and
policydetails but am getting some strange results!! Can somebody please
point me in the right direction.


<xsl:template match="message" >
<xsl:copy>
<xsl:for-each select="policydetails/fund" >

<xsl:copy-of select="../preceding-sibling::personal [1]" />

<policydetails>
<xsl:copy-of select="." />
</policydetails>
</xsl:for-each>
</xsl:copy>
</xsl:template>
This is two loops, one (implicit) for <message> and an explicit one for
<fnud>
We don't try to "synchronise" loops for people and funds, that never
works in XSLT. Instead we loop over whatever has the ordinal
relationship, then extract the dependent part (the <personal> with a
lookup).

There's also a typo in your XML <personal> / <person> and I don't like
its structure anyway. If <message> ever contains multiple <personal>s,
then you're relying on the _ordering_ of XML, not the nesting.
Personally this always makes me nervous.

Apr 5 '06 #4

<di*****@codesmiths.com> wrote in message
news:11********************@v46g2000cwv.googlegrou ps.com...
Iain wrote:
I have been trying with <xsl:for-each> looping through personal and
policydetails but am getting some strange results!! Can somebody please
point me in the right direction.
<xsl:template match="message" >
<xsl:copy>
<xsl:for-each select="policydetails/fund" >

<xsl:copy-of select="../preceding-sibling::personal [1]" />

<policydetails>
<xsl:copy-of select="." />
</policydetails>
</xsl:for-each>
</xsl:copy>
</xsl:template>
This is two loops, one (implicit) for <message> and an explicit one for
<fnud>
We don't try to "synchronise" loops for people and funds, that never
works in XSLT. Instead we loop over whatever has the ordinal
relationship, then extract the dependent part (the <personal> with a
lookup).

There's also a typo in your XML <personal> / <person> and I don't like
its structure anyway. If <message> ever contains multiple <personal>s,
then you're relying on the _ordering_ of XML, not the nesting.
Personally this always makes me nervous.


Many thanks. This worked a treat.

If <message> ever contains multiple <personal>s,
then you're relying on the _ordering_ of XML, not the nesting.
Personally this always makes me nervous.


I agree and understand. However I am limited to how the XML is structured
by the Filemaker database I need to import the data into.
Apr 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Oleg Konovalov | last post: by
6 posts views Thread by utterberg | last post: by
5 posts views Thread by Patrick.O.Ige | last post: by
1 post views Thread by jrwarwick | last post: by
2 posts views Thread by Scamjunk | last post: by
4 posts views Thread by shaun roe | last post: by
1 post views Thread by balderdash | last post: by
3 posts views Thread by IAMDkg | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.