Click to See Complete Forum and Search --> : Difficulty inserting DIV elements in XSL


hnmcc
July 30th, 2010, 09:06 AM
My project turns XML into HTML. I need to insert DIV elements in certain places and (so far) I haven't found a way of doing it.

My input XML, over which I have no control, looks like this:

<response>
<result>
<doc>
<str name="item_A">A</str>
<arr name="B">
<str>B1</str>
<str>B2</str>
...
</arr>
<str name="item_B">C</str>
<str name="item_D">D</str>
<str name="item_E">E</str>
...
</doc>
<doc>
...
</doc>
</result>
</response>


My XSL, which does almost everything I need, looks like this:

<body>
<div id="page">
<xsl:apply-templates select="response/result/doc"/>
</div>
</body>

<xsl:template match="doc/str">

<xsl:if test="@name='item_A'">
<div class="item_A">
<xsl:value-of select="."/>
</div>
</xsl:if>

<xsl:if test="@name='item_B'">
<div class="item_B">
<xsl:value-of select="."/>
</div>
</xsl:if>

...

</xsl:template>

<xsl:template match="doc/arr"/>
<xsl:template match="doc/date"/>

This probably looks a little cumbersome, but I must ignore some <str> nodes AND each item must have a different class. To achieve the control I need, I must insert at least one DIV (preferably two, nested) around all the output from each <doc> node. Such output would look like this, with the two new DIVs classed as "doc_node1" and "doc_node2":

<body>
<div id="page">
<div class="doc_node1">
<div class="doc_node2">
<div class="item_A">
content of item A
</div>
<div class="item_B">
content of item B
</div>
...
</div>
</div>
<div class="doc_node1">
<div class="doc_node2">
...
</div>
</div>
</div>
</body>


I've tried quite a few methods, all of which have failed. Can anyone tell me how to do this?