Whitespace node in XSLT

2019-08-10 23:05发布

问题:

I was going through the book XSLT 2.0 and XPath 2.0 by Michael Kay. I have gone through the topic of white space nodes. In this topic there is one example given.

<person>
  <name>Prudence Flowers</name>
  <employer>Lloyds Bank</employer>
  <place-of-work>
    71 Lombard Street
   London, UK
   <zip>EC3P 3BS</zip>
</place-of-work>
</person>

There are several text nodes present in the above XML.

  • Between starting of the <name> element and end of the <person> element.
  • Starting of the <name> and ending of the <employer> element
  • Starting of the <place-of-work> element and end of the <employer> element
  • End of the <zip> element and </place-of-work>.
  • </place-of-work> element and </person> element

If the below stylesheet is the stylesheet to transform the XML.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:strip-space elements="person"/>
</xsl:stylesheet>

Then my question is that if I execute the above XSLT file the it won't strip the whitespace node between the end of the <zip> element and </place-of-work>. Why?

回答1:

Then my question is that If I execute the above XSLT file the it wont strip the white space node between theEnd of the Zip element and </place-of-work>. why?

Because the parent of that text node is the place-of-work element, not the person element, and place-of-work is not one of the elements that you have specified as strip-space. The whitespace text nodes between <person> and <name> and between </name> and <employer> are direct children of the person element, so they will get stripped.



标签: xml xslt