Project

Profile

Help

source whitespace preserved on ignored tags

Added by Anonymous over 17 years ago

Legacy ID: #4073532 Legacy Poster: eb44 (eb44)

Using .NET 8B version (thank you for this utility; much appreciated). Transforming XML to flat file text. Source XML file has one tag per text line. Ignoring most tags via a template match. But getting a blank lines and indentation in output for every tag in input unless I used "-sall" option. Why is this required? Sorry for posting pseudo-samples but can't think of a way to describe the problem otherwise. XML (note indentation): <Record> <Tag1>data1</Tag1> <Tag2>data2</Tag2> <Tag3>data3</Tag3> </Record> XSL output-type has been set to text. XSL: <xsl:template match="Record"> ||<xsl:apply-templates/></xsl:template> <xsl:template match="Tag3"> <xsl:value-of select="name()"/>:<xsl:value-of select="."/></xsl:template> <xsl:template match="*"></xsl:template> In output, if I don't use -sall, I get indentations and blank lines: <blank line> <blank line> Tag3:data3 Why is -sall needed? This strips whitespace text nodes -- but those text nodes are matched by a template and shouldn't be in the output anyway. Thank you for your time and help.


Replies (4)

Please register to reply

RE: source whitespace preserved on ignored ta - Added by Anonymous over 17 years ago

Legacy ID: #4074762 Legacy Poster: Michael Kay (mhkay)

Are you sure you have a template rule that matches the whitespace text nodes? Because you haven't shown one. I strongly suspect they are being matched by the default template rule for text nodes, whose effect is to copy the text node to the output. Incidentally the standard way to strip whitespace text nodes from the source document in XSLT is <xsl:strip-space elements="*"/>. Saxon provides the -sall option mainly for XQuery which doesn't have any equivalent.

RE: source whitespace preserved on ignored ta - Added by Anonymous over 17 years ago

Legacy ID: #4079432 Legacy Poster: eb44 (eb44)

MK -- thank you for your reply. You can tell I'm not very experienced and I appreciate the tip on whitespace-stripping. Regarding a template for matching whitespace text nodes, I thought that I did show one: <xsl:template match="*"></xsl:template> My understanding was that this would match any node that wasn't covered by a more specific matching rule, and overrides the default rule. If that's the case, then I would have expected many nodes to be completely "ignored" because there is nothing between the opening and closing template tags. If this template is NOT matching other nodes, or if the match is still causing values to be copied to the output, then I need to do some (more!) homework in order to understand xslt rules. Again, many thanks for your utility and your time.

RE: source whitespace preserved on ignored ta - Added by Anonymous over 17 years ago

Legacy ID: #4079685 Legacy Poster: Michael Kay (mhkay)

The template rule <xsl:template match="*"></xsl:template> doesn't match text nodes, it matches element nodes only. When you do <xsl:apply-templates/> in your match="Record" template rule, it processes all the child nodes of the Record element. These include further elements, for which you provide template rules, and whitespace text nodes, which are processed by the built-in template rule for text nodes. The built-in template rule copies text nodes unchanged to the result tree.

RE: source whitespace preserved on ignored ta - Added by Anonymous over 17 years ago

Legacy ID: #4079900 Legacy Poster: eb44 (eb44)

Thank you. That helps a lot. I didn't understand the difference between "element" and "text" nodes, but it's making more sense now. FYI, I had started with an interactive-only translator (Architag's XRay), which did (incorrectly, it appears) suppress the unwanted whitespace text nodes as a result of the element-match "*" template. So I was confused about the difference in behavior between their engine and yours. Sounds like it's time for me to pick up a book on XSLT...I bet you have a recommendation! Is your 2004 book the way to go, or will there be an update coming in the not-too-distant future. I appreciate your time and patience.

    (1-4/4)

    Please register to reply