How filter data from XML to XML

L

Luca

Hello,

have a problem with this XML, I need XSLT to filter the products list
based on rules writes in RULE tag.

I need copy only CAT and PROD data in PRODUCTS-LIST that match CAT and
PROD ID's in RULE tag.

for ex:

==========================================================================
<ROOT>
<PAGE NAME="homepage" URL="HP.html">
<RULES>
<RULE>
<CAT ID="1">
<PRO ID="3"></PRO>
<PRO ID="6"></PRO>
</CAT>
<CAT ID="3">
<PRO ID="2"></PRO>
<PRO ID="10"></PRO>
</CAT>
</RULE>
</RULES>
</PAGE>
<PRODUCTS-LIST>
<CAT ID="1">
<PROD ID="12">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="6">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="3">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
<CAT ID="2">
<PROD ID="7">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
<CAT ID="3">
<PROD ID="10">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="1">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="2">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
</PRODUCTS-LIST>
</ROOT>


==========================================================================
the final result of XSLT trasf.

==========================================================================
<ROOT>
<PRODUCTS-LIST>
<CAT ID="1">
<PROD ID="6">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="3">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
<CAT ID="3">
<PROD ID="10">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="2">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
</PRODUCTS-LIST>
</ROOT>

=========================================================================

Can anybody help me?

Thanks for answers!
Luca
 
D

Dimitre Novatchev

This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="ROOT">
<xsl:copy>
<xsl:apply-templates select="PRODUCTS-LIST"/>
</xsl:copy>
</xsl:template>

<xsl:template match="PRODUCTS-LIST">
<xsl:copy>
<xsl:apply-templates
select="CAT[@ID = /*/*/*/RULE/CAT/@ID]"/>
</xsl:copy>
</xsl:template>

<xsl:template match="CAT">
<xsl:copy-of select=
"PROD[@ID = /*/*/*/RULE/CAT
[@ID = current()/@ID]/PRO/@ID]"/>
</xsl:template>
</xsl:stylesheet>

when applied on your source.xml:

<ROOT>
<PAGE NAME="homepage" URL="HP.html">
<RULES>
<RULE>
<CAT ID="1">
<PRO ID="3"></PRO>
<PRO ID="6"></PRO>
</CAT>
<CAT ID="3">
<PRO ID="2"></PRO>
<PRO ID="10"></PRO>
</CAT>
</RULE>
</RULES>
</PAGE>
<PRODUCTS-LIST>
<CAT ID="1">
<PROD ID="12">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="6">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="3">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
<CAT ID="2">
<PROD ID="7">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
<CAT ID="3">
<PROD ID="10">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="1">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="2">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
</PRODUCTS-LIST>
</ROOT>

produces the wanted result:

<ROOT>
<PRODUCTS-LIST>
<PROD ID="6">
<NAME>Prod. name</NAME>
<DESC> Html desc </DESC>
</PROD>
<PROD ID="3">
<NAME>Prod. name</NAME>
<DESC> Html desc </DESC>
</PROD>
<PROD ID="10">
<NAME>Prod. name</NAME>
<DESC> Html desc </DESC>
</PROD>
<PROD ID="2">
<NAME>Prod. name</NAME>
<DESC> Html desc </DESC>
</PROD>
</PRODUCTS-LIST>
</ROOT>


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

Luca said:
Hello,

have a problem with this XML, I need XSLT to filter the products list
based on rules writes in RULE tag.

I need copy only CAT and PROD data in PRODUCTS-LIST that match CAT and
PROD ID's in RULE tag.

for ex:

==========================================================================
<ROOT>
<PAGE NAME="homepage" URL="HP.html">
<RULES>
<RULE>
<CAT ID="1">
<PRO ID="3"></PRO>
<PRO ID="6"></PRO>
</CAT>
<CAT ID="3">
<PRO ID="2"></PRO>
<PRO ID="10"></PRO>
</CAT>
</RULE>
</RULES>
</PAGE>
<PRODUCTS-LIST>
<CAT ID="1">
<PROD ID="12">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="6">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="3">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
<CAT ID="2">
<PROD ID="7">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
<CAT ID="3">
<PROD ID="10">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="1">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="2">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
</PRODUCTS-LIST>
</ROOT>


==========================================================================
the final result of XSLT trasf.

==========================================================================
<ROOT>
<PRODUCTS-LIST>
<CAT ID="1">
<PROD ID="6">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="3">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
<CAT ID="3">
<PROD ID="10">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
<PROD ID="2">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>
</CAT>
</PRODUCTS-LIST>
</ROOT>

=========================================================================

Can anybody help me?

Thanks for answers!
Luca
 
L

luca milan

Thanks Dimitre,

very useful, last suggetstion
I need nest PRODUCTS-LISTS into each CAT tag:

<CAT ID="1">
<PROD ID="6">
<NAME>Prod. name</NAME>
<DESC><![CDATA[ Html desc ]]></DESC>
</PROD>

<!-- and so on.... -->

</CAT>


Thanks a lot for any Help!!!
 
L

luca milan

sorry Dimitre, simply add this:

<CAT ID="{@ID}">
<xsl:copy-of select="PROD[@ID = /*/*/*/RULE/CAT[@ID =
current()/@ID]/PRO/@ID]"/>
</CAT>

in CAT template...

Thanks a lot for any Help!!!
 
D

Dimitre Novatchev

luca milan said:
sorry Dimitre, simply add this:

<CAT ID="{@ID}">
<xsl:copy-of select="PROD[@ID = /*/*/*/RULE/CAT[@ID =
current()/@ID]/PRO/@ID]"/>
</CAT>

in CAT template...

Thanks a lot for any Help!!!

Yes, sure -- I did forget to copy the CAT element itself -- sorry, it's
almost midnight here.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 
L

luca milan

No problem Dimitre,

but I have a last question, if I add others rules

<RULE NAME="fisrt">
<CAT ID="13">
<PRO ID="17"></PRO>
<PRO ID="14"></PRO>
</CAT>
</RULE>
<RULE NAME="second">
<CAT ID="13">
<PRO ID="17"></PRO>
<PRO ID="14"></PRO>
</CAT>
</RULE>

and so... I need group by rules the procucts list, for ex:

<RULE NAME="first">
CAT and PROD markup... for related rule
</RULE>
<RULE NAME="second">
CAT and PROD markup... for related rule
</RULE>

I can do this... now try...

Thanks a lot for any Help!!!
 
D

Dimitre Novatchev

luca milan said:
No problem Dimitre,

but I have a last question, if I add others rules

<RULE NAME="fisrt">
<CAT ID="13">
<PRO ID="17"></PRO>
<PRO ID="14"></PRO>
</CAT>
</RULE>
<RULE NAME="second">
<CAT ID="13">
<PRO ID="17"></PRO>
<PRO ID="14"></PRO>
</CAT>
</RULE>

and so... I need group by rules the procucts list, for ex:

<RULE NAME="first">
CAT and PROD markup... for related rule
</RULE>
<RULE NAME="second">
CAT and PROD markup... for related rule
</RULE>

I can do this... now try...

Anything can be done, but you're again too cryptic in explaining what you
want and I do not understand it.

Please, get accustomed to always provide a complete (but minimal) example
with a good explanation. In all other cases people start guessing what you
actually mean and want.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 
L

luca milan

I add this xml, for ex:

==========================================================
<ROOT>
<PAGE NAME="homepage" url="hp.html">
<RULES>
<RULE NAME="first">
<CAT id="1">
<PRO id="3"></PRO>
<PRO id="6"></PRO>
</CAT>
</RULE>
<RULE NAME="second">
<CAT id="3">
<PRO id="1"></PRO>
</CAT>
</RULE>
</RULES>
</PAGE>
<PRODUCTS-LIST>
<CAT id="1">
<PROD id="12">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="6">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="3">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
<CAT id="2">
<PROD id="7">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
<CAT id="3">
<PROD id="10">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="1">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="2">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
</PRODUCTS-LIST>
</ROOT>
==========================================================

desiderd result:

==========================================================

<ROOT>
<PRODUCTS-LIST>
<RULE NAME="first">
<CAT id="1">
<PROD id="6">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="3">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
</RULE>
<RULE NAME="second">
<CAT id="3">
<PROD id="1">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
</RULE>
</PRODUCTS-LIST>
</ROOT>

==========================================================


Thanks a lot for any Help!!!
 
D

Dimitre Novatchev

Here it is:

This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext"
<xsl:eek:utput omit-xml-declaration="yes" indent="yes"
cdata-section-elements="DESC"/>

<xsl:strip-space elements="*"/>

<xsl:template match="/">
<ROOT>
<PRODUCTS-LIST>
<xsl:apply-templates select="*/*/RULES"/>
</PRODUCTS-LIST>
</ROOT>
</xsl:template>

<xsl:template match="RULES">
<xsl:variable name="vOut">
<xsl:for-each select="RULE">
<RULE NAME="{@NAME}">
<xsl:for-each select="CAT">
<xsl:if test="/*/PRODUCTS-LIST/CAT
[@id = current()/@id]
[PROD[@id = current()/PRO/@id]]">
<CAT id="{@id}">
<xsl:copy-of select=
"/*/PRODUCTS-LIST/CAT
[@id = current()/@id]
/PROD
[@id = current()/PRO/@id]"/>
</CAT>
</xsl:if>
</xsl:for-each>
</RULE>
</xsl:for-each>
</xsl:variable>

<xsl:copy-of select="ext:node-set($vOut)/*[*]"/>
</xsl:template>
</xsl:stylesheet>


when applied on your source.xml:

<ROOT>
<PAGE NAME="homepage" url="hp.html">
<RULES>
<RULE NAME="first">
<CAT id="1">
<PRO id="3"></PRO>
<PRO id="6"></PRO>
</CAT>
</RULE>
<RULE NAME="second">
<CAT id="3">
<PRO id="1"></PRO>
</CAT>
</RULE>
</RULES>
</PAGE>
<PRODUCTS-LIST>
<CAT id="1">
<PROD id="12">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="6">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="3">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
<CAT id="2">
<PROD id="7">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
<CAT id="3">
<PROD id="10">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="1">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="2">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
</PRODUCTS-LIST>
</ROOT>


produces the wanted result:

<ROOT>
<PRODUCTS-LIST>
<RULE NAME="first">
<CAT id="1">
<PROD id="6">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="3">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
</RULE>
<RULE NAME="second">
<CAT id="3">
<PROD id="1">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
</RULE>
</PRODUCTS-LIST>
</ROOT>


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

luca milan said:
I add this xml, for ex:

==========================================================
<ROOT>
<PAGE NAME="homepage" url="hp.html">
<RULES>
<RULE NAME="first">
<CAT id="1">
<PRO id="3"></PRO>
<PRO id="6"></PRO>
</CAT>
</RULE>
<RULE NAME="second">
<CAT id="3">
<PRO id="1"></PRO>
</CAT>
</RULE>
</RULES>
</PAGE>
<PRODUCTS-LIST>
<CAT id="1">
<PROD id="12">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="6">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="3">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
<CAT id="2">
<PROD id="7">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
<CAT id="3">
<PROD id="10">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="1">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="2">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
</PRODUCTS-LIST>
</ROOT>
==========================================================

desiderd result:

==========================================================

<ROOT>
<PRODUCTS-LIST>
<RULE NAME="first">
<CAT id="1">
<PROD id="6">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
<PROD id="3">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
</RULE>
<RULE NAME="second">
<CAT id="3">
<PROD id="1">
<NAME>PROD. NAME</NAME>
<DESC><![CDATA[ Html DESC ]]></DESC>
</PROD>
</CAT>
</RULE>
</PRODUCTS-LIST>
</ROOT>

==========================================================


Thanks a lot for any Help!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top