Reputation: 19
I'm trying to transform 1 xml file(file1.xml) to change it's tags and at the same time i want to replace value on 1 tag(category) with value from another XML file(replace.xml) by comparing if the @id of "file1.xml" is equal to @id from "replace.xml". If true then replace value of that TAG(category) in file1.xml with value from "replace.xml", but i dont know how to do it in 1 XSLT file so i am trying to make 2 files which will transform:
File1.xml
<?xml version="1.0"?>
<products>
<product>
<id_item>1002333</id_item>
<producer>XYZ</producer>
<price>
<delivery_cost>
<prepayment>000</prepayment>
<cash_on_delivery>000</cash_on_delivery>
</delivery_cost>
</price>
<category id="456" name="Gloves"></category>
</product>
<product>
<id_item>1002000</id_item>
<producer>XYZ</producer>
<price>
<delivery_cost>
<prepayment></prepayment>
<cash_on_delivery></cash_on_delivery>
</delivery_cost>
</price>
<category id="123" name="Shoes"></category>
</product>
</products>
i transformed this Example above with my first XSL(i cant include that file here because it is too big and i dont know how to make an example of it)
Output of the first transformation:
<?xml version="1.0"?>
<channel>
<PRODUCT>
<ID_ITEM>1002333</ID_ITEM>
<PRODUCER>XYZ</PRODUCER>
<PRICE_VAT>000</PRICE_VAT>
<CATEGORY id="456">Gloves</CATEGORY >
</PRODUCT>
<PRODUCT>
<ID_ITEM>1002000</ID_ITEM>
<PRODUCER>XYZ</PRODUCER>
<PRICE_VAT>000</PRICE_VAT>
<CATEGORY id="123">Shoes</CATEGORY >
</PRODUCT>
</channel>
and now i want to use this XML file (replace.xml) to change the text value of tag CATEGORY of Output above with value from (replace.xml)
Replace.xml
<?xml version="1.0"?>
<ITEM>
<category id="456">ReplacedGloves</category>
<category id="123">ReplacedShoes</category>
<category id="321">REplaced1</category>
<category id="432">REplaced2</category>
</ITEM>
with 2nd XSL file
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:import href="firstXSLhere.xsl"/>
<xsl:variable name="plik_mapping" select="document('maptest.xml')"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="category">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:choose>
<xsl:when test="$plik_mapping//category[@id=current()/@id]">
<xsl:apply-templates select="$plik_mapping//category[@id=current()/@id]/@name"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="@name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
and final output
<?xml version="1.0"?>
<channel>
<PRODUCT>
<ID_ITEM>1002333</ID_ITEM>
<PRODUCER>XYZ</PRODUCER>
<PRICE_VAT>000</PRICE_VAT>
<CATEGORY id="456">ReplacedGloves</CATEGORY >
</PRODUCT>
<PRODUCT>
<ID_ITEM>1002000</ID_ITEM>
<PRODUCER>XYZ</PRODUCER>
<PRICE_VAT>000</PRICE_VAT>
<category id="123">ReplacedShoes</CATEGORY >
</PRODUCT>
</channel>
what i'm trying to do here is importing the first XSL file to the 2nd XSL file and transform File1.xml to it's final output. But im not sure what im doing wrong here. Since the File1.xml only transform with the first XSL file templates. I'm not sure if i did the order of import precedence in wrong way.
Upvotes: 0
Views: 79
Reputation: 117003
As I told you in the previous version of this question, you can probably do the entire transformation using a single stylesheet.
Since you did not post the 1st XSLT, it id difficult to say exactly how this should be done. Here is a simplified example:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:param name="plik_mapping" select="document('maptest.xml')"/>
<xsl:template match="/products">
<channel>
<xsl:for-each select="product">
<PRODUCT>
<ID_ITEM>
<xsl:value-of select="id_item" />
</ID_ITEM>
<PRODUCER>
<xsl:value-of select="producer" />
</PRODUCER>
<PRICE_VAT>
<!-- ??? -->
</PRICE_VAT>
<xsl:variable name="cat" select="category" />
<xsl:variable name="replacement" select="$plik_mapping/ITEM/category[@id=$cat/@id]" />
<CATEGORY id="{$cat/@id}">
<xsl:choose>
<xsl:when test="$replacement">
<xsl:value-of select="$replacement" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$cat/@name" />
</xsl:otherwise>
</xsl:choose>
</CATEGORY>
</PRODUCT>
</xsl:for-each>
</channel>
</xsl:template>
</xsl:stylesheet>
Applied to your input XML example, this will produce:
Result
<?xml version="1.0"?>
<channel>
<PRODUCT>
<ID_ITEM>1002333</ID_ITEM>
<PRODUCER>XYZ</PRODUCER>
<PRICE_VAT/>
<CATEGORY id="456">ReplacedGloves</CATEGORY>
</PRODUCT>
<PRODUCT>
<ID_ITEM>1002000</ID_ITEM>
<PRODUCER>XYZ</PRODUCER>
<PRICE_VAT/>
<CATEGORY id="123">ReplacedShoes</CATEGORY>
</PRODUCT>
</channel>
which is similar to the output you show, except for the value of PRICE_VAT
for which you gave us no information regarding how it should be derived (and of course the category
vs. CATEGORY
mismatch).
Upvotes: 0