Re: [xsl] Newline problems

Subject: Re: [xsl] Newline problems
From: "Vishwajit Pantvaidya" <pantvaidya@xxxxxxxxxxx>
Date: Mon, 05 May 2003 21:31:37 -0700
Mike - thanks for persisting so long with me.

From: Mike Brown <mike@xxxxxxxx>
Date: Mon, 5 May 2003 18:31:43 -0600 (MDT)

Vishwajit Pantvaidya wrote:
> >Look at your XML. The only text node *children* of the info element
> >are whitespace. There are some other child nodes that are elements
> >and *those* have text node children. But you are not selecting any
> >of those.
> Does this mean that I have to reply the xsl:value-of for ponumber with
> apply-templates and then do the normalize-space in that template processing.

Yes, if I understand your question correctly. xsl:apply-templates means
"process these nodes, using the best matching template for each one"... the
built-in template for element nodes just calls <xsl:apply-templates/> which
means to process the children of the element.. the built-in template for text
nodes just does <xsl:copy/> which copies the node. You'll override that
template for text nodes with a template that does <xsl:value-of
select="normalize-space(.)"/>, which creates a new text node with the
normalized string.

> But apart from ponumber, I am reading other data similarly, (which also has
> newlines) and does this mean that I have to have a template for each of such
> data attributes?

If the data is in a text node, then no. You just have to make sure to use
xsl:apply-templates to get to the text nodes instead of using xsl:value-of on
their parent element.

> Is there an optimal way of not only removing newlines from
> all such data items but also copying them into the output xml?

You are not modifying the source tree in XSLT. You are only creating the
result tree from which the output XML is automatically derived after the
stylesheet is processed.

Here just to explain, I am inserting a simplified version of the original xml and xsl:

<atomicValue>PO BOX 1234</atomicValue>
<atomicValue>MEDICAL CENTER</atomicValue>

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:transform xmlns:xsl = ""; version = "1.0">

<xsl:template match = "Quote">
<xsl:text disable-output-escaping="yes">&lt;!DOCTYPE Order&gt;</xsl:text>
<xsl:element name = "Order">
<xsl:element name ="Info">
<xsl:apply-templates mode = "Attribs" select = "/Quote/Info"/>

<xsl:template mode = "Attribs" match = "attribute"> <xsl:element name="BILL_TO_ADDRESS2"> <xsl:value-of select="attribute/atomicValue[../name='BILL_TO_ADDRESS2']"/> </xsl:element> <xsl:element name="PO_NUMBER"> <xsl:value-of select="attribute/atomicValue[../name='PO_NUMBER']"/> </xsl:template> </xsl:transform>

I guess I could have used a common template to copy all name nodes and their atomicvalues to the output tree, but that would copy all attributes from the source in the output, which I do not want e.g. the above snippet excludes attribute with name BILL_TO_ADDRESS3 that is present in the source xml.

Also you will see that I need only the atomicvalues in the output xml. So the way the logic gets to them is through the name nodes i.e. xsl:value-of select "the atomicvalue for the node whose name is 'BILL_TO_ADDRESS2'". It seems that the logic is failing as the processor finds that the values of the name nodes are '<newline>BILL_TO_ADDRESS2' which hence does not match.

How can I achieve these objectives by using templates?



STOP MORE SPAM with the new MSN 8 and get 2 months FREE*

XSL-List info and archive:

Current Thread