Home > database >  Transform XML element if id matches with id on the list from another xml file
Transform XML element if id matches with id on the list from another xml file

Time:09-26

I'm sorry if the title is not well formed. I have no idea how to make it more precisely to the problem. I am trying to transform XML file(file_A.xml) like this example:

<?xml version="1.0" encoding="utf-8"?>
<offer>
    <products>
        <product>
            <producer/>
            <category/>
            <description>
                <name>name</name>
                <short_desc>short description</short_desc>
            </description>
            <price/>
            <sizes/>
            <images/>
            <iaiext:priority_menu>
                <site id="1">
                    <menu id="1">
                        <item id="757" textId="KOBIETA\Obuwie\Szpilki" level="40000"/>
                    </menu>
                </site>
            </iaiext:priority_menu>
        </product>
        
        <product>
            <producer/>
            <category/>
            <description>
                <name>name</name>
                <short_desc>short description</short_desc>
            </description>
            <price/>
            <sizes/>
            <images/>
            <iaiext:priority_menu>
                <site id="1">
                    <menu id="1">
                        <item id="888" textId="KOBIETA\Obuwie\Szpilki" level="40000"/>
                    </menu>
                </site>
            </iaiext:priority_menu>
        </product>
        
        <product>
        ...
        </product>
        
        <product>
        ...
        </product>
    </products>
</offer>

I want to select only products that has item id matches with the list of id on another xml file(file_B.xml) that looks like this:

<?xml version="1.0"?>
<FilterList>
  <category_id id="174" name="Botki"/>
  <category_id id="183" name="Kalosze"/>
  <category_id id="757" name="Szpilki"/>
  <category_id id="173" name="Sandały"/>
  <category_id id="185" name="Espadryle"/>
  <category_id id="209" name="Klapki"/>
  <category_id id="818" name="Kapcie"/>
  <category_id id="756" name="Workery"/>
  <category_id id="204" name="Trapery"/>
  <category_id id="206" name="Śniegowce"/>
</FilterList>

I managed to be able to get item with specific ID by using this in xslt:

<xsl:for-each select="offer/products/*">
                <xsl:if test="(description/name and description/name != ''and (iaiext:priority_menu/site/menu/item[@id = '757']))">
                </xsl:if>
</xsl:for-each>

But i have no idea how to get all items from file_A.xml that matches any id from the item's id on the file_B.xml

CodePudding user response:

Consider the following example:

XML

<offer>
    <products>
        <product>
            <producer/>
            <category/>
            <description>
                <name>name</name>
                <short_desc>short description</short_desc>
            </description>
            <price/>
            <sizes/>
            <images/>
            <priority_menu>
                <site id="1">
                    <menu id="1">
                        <item id="757" textId="KOBIETA\Obuwie\Szpilki" level="40000"/>
                    </menu>
                </site>
            </priority_menu>
        </product>
        <product>
            <producer/>
            <category/>
            <description>
                <name>name</name>
                <short_desc>short description</short_desc>
            </description>
            <price/>
            <sizes/>
            <images/>
            <priority_menu>
                <site id="1">
                    <menu id="1">
                        <item id="888" textId="KOBIETA\Obuwie\Szpilki" level="40000"/>
                    </menu>
                </site>
            </priority_menu>
        </product>
     </products>
</offer>

file_B.xml

<FilterList>
  <category_id id="174" name="Botki"/>
  <category_id id="183" name="Kalosze"/>
  <category_id id="757" name="Szpilki"/>
  <category_id id="173" name="Sandały"/>
  <category_id id="185" name="Espadryle"/>
  <category_id id="209" name="Klapki"/>
  <category_id id="818" name="Kapcie"/>
  <category_id id="756" name="Workery"/>
  <category_id id="204" name="Trapery"/>
  <category_id id="206" name="Śniegowce"/>
</FilterList>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="product" match="product" use="priority_menu/site/menu/item/@id" />

<xsl:template match="/offer">
    <output>
        <xsl:for-each select="key('product', document('file_B.xml')/FilterList/category_id/@id)">
             <xsl:copy-of select="."/>  
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

Result

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <product>
      <producer/>
      <category/>
      <description>
         <name>name</name>
         <short_desc>short description</short_desc>
      </description>
      <price/>
      <sizes/>
      <images/>
      <priority_menu>
         <site id="1">
            <menu id="1">
               <item id="757" textId="KOBIETA\Obuwie\Szpilki" level="40000"/>
            </menu>
         </site>
      </priority_menu>
   </product>
</output>
  • Related