I'm new with xml, it's very difficult for me to understand how XSLT works. Can you help me fix some errors in my xslt file? I'd like to transform this input file :
<?xml version="1.0" encoding="utf-8"?>
<data>
<parent><string >AAA</string></parent>
<nb><string >2</string></nb>
<child1>aaa-1</child1>
<child1>aaa-2</child1>
<parent><string >BBB</string></parent>
<nb><string>1</string></nb>
<child2>bbb-1</child2>
<parent><string >CCC</string></parent>
<nb><string >0</string></nb>
</data>
into :
<?xml version="1.0" encoding="utf-8"?>
<data>
<parent>
<string >AAA</string>
<nb><string >2</string></nb>
<child1>aaa-1</child1>
<child1>aaa-2</child1></parent>
<parent>
<string >BBB</string>
<nb><string >1</string></nb>
<child2>bbb-1</child2></parent>
<parent>
<string >CCC</string>
<nb><string >0</string></nb></parent>
</data>
The rules are :
The "nb" node indicates the number of childs for each parent. It can be 0.
The nodes "child1" and "child2" are different. They are complex, with nested loops etc. My above input file is simplified for demo.
I think, I've to use the 'copy' instruction.What is difficult for me, is that :
- for each parent, I've to read the defined number ("nb") of child nodes following current parent
- when the "parent" value is 'AAA' then I've to read the "child1"
when the "parent" value is !=AAAA then I've to read the "childe2" node.
Here is my XSLT file, the result is not exactly as expected :
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<xsl:comment>--- </xsl:comment>
<xsl:comment>1 : parent nodes </xsl:comment>
<xsl:for-each select ="/data/parent">
<p>
<string>
<xsl:value-of select="string"/>
</string>
<xsl:comment>======================= </xsl:comment>
<xsl:comment>2 : nb nodes (how many childs for a parent ) </xsl:comment>
<xsl:for-each select ="/data/nb">
<xsl:if test="((position() < 2) and (normalize-space(position() >= 1)))">
<xsl:comment>Ex. for tThe first value only </xsl:comment>
<xsl:comment>How to do a dynamic test here (expected : AAA->3 (first nb value), BBB->1 (second nb value) ...) ?</xsl:comment>
<xsl:comment>How to synchronise loop on parent and nb ?</xsl:comment>
<nb>
<string>
<xsl:value-of select="string"/>
</string>
</nb>
</xsl:if>
</xsl:for-each>
<xsl:comment>======================= </xsl:comment>
<xsl:comment>3 : child nodes </xsl:comment>
<xsl:comment>How to manage the position and number of nodes to read ?</xsl:comment>
<xsl:comment>Test 'string =AAA' is KO : always child2 </xsl:comment>
<xsl:choose>
<xsl:when test='string =AAA'>
<xsl:copy-of select="/*/child1" />
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="/*/child2" />
</xsl:otherwise>
</xsl:choose>
</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Thanks in advance
Best regards