Subject: Re: unfold one element to several elements according to its attribute
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sun, 25 May 2008 23:07:03 +0530
|
Following is a working 2.0 stylesheet somewhat similar to Martin's solution:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="root">
<result>
<xsl:apply-templates select="product" />
</result>
</xsl:template>
<xsl:template match="product">
<xsl:variable name="x" select="." />
<xsl:for-each select="tokenize(@owner,',')">
<product>
<xsl:copy-of select="$x/@*[not(name() = 'owner')]" />
<xsl:attribute name="owner">
<xsl:value-of select="." />
</xsl:attribute>
<title>
<xsl:value-of select="$x/title[contains(@owner, current())]" />
</title>
</product>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This when applied to the XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<product id="1" owner="a,b,c">
<title owner="a,b">foo</title>
<title owner="c">bar</title>
</product>
<product id="2" owner="p,q">
<title owner="q">abc</title>
<title owner="p">pqr</title>
</product>
</root>
produces output:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<product id="1" owner="a">
<title>foo</title>
</product>
<product id="1" owner="b">
<title>foo</title>
</product>
<product id="1" owner="c">
<title>bar</title>
</product>
<product id="2" owner="p">
<title>pqr</title>
</product>
<product id="2" owner="q">
<title>abc</title>
</product>
</result>
On Sun, May 25, 2008 at 10:36 PM, mixhere <mixhere@xxxxxxxxx> wrote:
> Hello everyone,
> is it possible to do the translation? you can add other
> elements/attributes in the original xml if the info is not enough.
> thanks.
>
> Original:
> <product id="1" owner="a,b,c">
> <title owner="a,b">foo</title>
> <title owner="c">bar</title>
> </product>
>
> After translation:
> <product id="1" owner="a">
> <title>foo</title>
> </product>
> <product id="1" owner="b">
> <title>foo</title>
> </product>
> <product id="1" owner="c">
> <title>bar</title>
> </product>
--
Regards,
Mukul Gandhi
|