XSL Multiple sub-items

Here should go questions about transforming XML with XSLT and FOP.
zesuisse
Posts: 1

XSL Multiple sub-items

Fri Apr 01, 2016 7:59 pm

Hello,

Having the following xml:

Code: Select all

<ns:Layout tableName="AssetRecords">
<ns:Fields>
<ns:Field uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}" type="0" valueInterpretation="0">
<ns:Name>Categories</ns:Name>
</ns:Field>
<ns:Field uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}" type="2" valueInterpretation="0">
<ns:Name>ID</ns:Name>
</ns:Field>
</ns:Fields>
</ns:Layout>
<ns:Items>
<ns:Item catalogid="10" id="4">
<ns:FieldValue uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}">
<ns:CategoryValue>$Categories:Cat1</ns:CategoryValue>
<ns:CategoryValue>$Sources:System:Users:alaindefrasne:Desktop</ns:CategoryValue>
</ns:FieldValue>
<ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">4</ns:FieldValue>
</ns:Item>
</ns:Items>
I managed to get a text file which looks like:

Code: Select all

$Categories:Cat1|$Sources:System:Users:alaindefrasne:Desktop<TAB>4
But I need to get this (actually one line for each CategoryValue with the corresponding ID)

Code: Select all

$Categories:Cat1<TAB>4
$Sources:System:Users:alaindefrasne:Desktop<TAB>4
The used xslt is as follow:

Code: Select all

<!-- Parameterized separator/end of line characters for flexibility -->
<xsl:param name="sep" select="'&#09;'" />
<xsl:param name="eol" select="'&#10;'" />
<xsl:param name="listsep" select="'|'" />
<xsl:param name="catsep" select="'&#xa;'" />


<!-- On matching the root node, output a list of field names, followed by the items -->
<xsl:template match="/ns:Export">
<xsl:apply-templates select="ns:Layout/ns:Fields/ns:Field" />
<xsl:apply-templates select="ns:Items/ns:Item"/>
</xsl:template>

<!-- On matching all but the last field name, output the name followed by separator -->
<xsl:template match="ns:Field[position()!=last()]">
<xsl:value-of select="concat(normalize-space(ns:Name),$sep)" />
</xsl:template>

<!-- On matching the last field name, output the name followed by a newline -->
<xsl:template match="ns:Field[position()=last()]">
<xsl:value-of select="concat(normalize-space(ns:Name),$eol)" />
</xsl:template>

<!-- On matching an item, iterate through each field, applying templates to any 'ns:FieldValue' nodes that share the same value of @uid -->
<xsl:template match="ns:Item">
<xsl:variable name="item" select="." />
<xsl:for-each select="/ns:Export/ns:Layout/ns:Fields/ns:Field/@uid">
<xsl:apply-templates select="$item/ns:FieldValue[@uid=current()]" />
<xsl:if test="position()!=last()">
<xsl:value-of select="$sep" />
</xsl:if>
</xsl:for-each>
<xsl:value-of select="$eol" />
</xsl:template>

<!-- On matching a field value, output the content. -->
<xsl:template match="ns:FieldValue">
<xsl:value-of select="normalize-space(.)" />
</xsl:template>

<!-- on matching a field value with a @displayValue attribute, output the value of that attribute -->
<xsl:template match="ns:FieldValue[@displayValue]">
<xsl:value-of select="normalize-space(@displayValue)" />
</xsl:template>

<!-- On matching a field value with ns:CategoryValue children, apply templates on those children. -->
<xsl:template match="ns:FieldValue[ns:CategoryValue]">
<xsl:apply-templates select="ns:CategoryValue" />
</xsl:template>

<!-- On matching a category value, output it's content, plus a separator. -->
<xsl:template match="ns:CategoryValue">
<xsl:value-of select="concat(normalize-space(.),$listsep)" />
</xsl:template>
Thanks in advance.
Best,
Alain.

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 0 guests