No ale to się sprawdzi w twoim przypadku
W kilku krokach:
#1 PRZYGOTOWANIE DOKUMENTU XML DLA ZADANEGO XML-A
wzorzec XML
<farby>
<farba1>
<cechy>
<typ></typ>
</cechy>
<cena></cena>
<nazwa>
<skrocona></skrocona>
</nazwa>
</farba1>
</farby>
XSLT dla wzorca:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name = "xsl:stylesheet" namespace = "http://www.w3.org/1999/XSL/Transform" >
<xsl:attribute name="version">1.0</xsl:attribute>
<xsl:element name = "xsl:output" namespace = "http://www.w3.org/1999/XSL/Transform" >
<xsl:attribute name="method">xml</xsl:attribute>
<xsl:attribute name="indent">yes</xsl:attribute>
</xsl:element>
<xsl:element name = "xsl:template" namespace = "http://www.w3.org/1999/XSL/Transform" >
<xsl:attribute name="match">/</xsl:attribute>
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(*)]">
<xsl:element name = "{name(.)}" >
<xsl:element name = "xsl:value-of" >
<xsl:attribute name="select">
<xsl:call-template name="genPath"/>
</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template name="genPath">
<xsl:param name="prevPath"/>
<xsl:variable name="currPath" select="concat('/',name(),'[', count(preceding-sibling::*[name() = name(current())])+1,']',$prevPath)"/>
<xsl:for-each select="parent::*">
<xsl:call-template name="genPath">
<xsl:with-param name="prevPath" select="$currPath"/>
</xsl:call-template>
</xsl:for-each>
<xsl:if test="not(parent::*)">
<xsl:value-of select="$currPath"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Wynik transformacji docelowy xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<farby>
<farba1>
<cechy>
<typ><xsl:value-of select="/farby[1]/farba1[1]/cechy[1]/typ[1]"/></typ>
</cechy>
<cena><xsl:value-of select="/farby[1]/farba1[1]/cena[1]"/></cena>
<nazwa>
<skrocona><xsl:value-of select="/farby[1]/farba1[1]/nazwa[1]/skrocona[1]"/></skrocona>
</nazwa>
</farba1>
</farby>
</xsl:template>
</xsl:stylesheet>
#2 Przetwarzanie zadanego xml-a
Zadany xml transformuje wynikiem z #1 i uzyskuje:
<farby>
<farba1>
<cechy>
<typ>emalia</typ>
</cechy>
<cena>100</cena>
<nazwa>
<skrocona>slonce</skrocona>
</nazwa>
</farba1>
</farby>
**3# wykorzystanie w programie **
Stały jest tylko arkusz xsl dla pliku wzorca, reszta to dynamiczne pliki:
W pseudo kodzie C#
załóżmy, że
wzorzec to plik wzorzec.xml
arkusz xsl dla wzorca wzorzec.xsl
zadany xml to input.xml
https://support.microsoft.com/pl-pl/kb/307322
XslTransform myXslTransform;
myXslTransform = new XslTransform();
//przygotuj xsl na podstawie wzorca
myXslTransform.Load("wzorzec.xsl");
myXslTransform.Transform("wzorzec.xml", "input.xsl");
//przetworz input
myXslTransform.Load("input.xsl");
myXslTransform.Transform("input.xml", "output.xml");