XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
Re: [xml-dev] XSLT filter, delete XML elements without leaving blank holes?

Just add this template and remove the indent="yes" attribute of <xsl:output>:

    <xsl:template
match="text()[following-sibling::node()[1][self::maybe[remove]]]" />

The SO answer is here:
   http://stackoverflow.com/a/35543212/36305


Cheers,
Dimitre

On Sun, Feb 21, 2016 at 2:26 PM, Jim DeLaHunt <from.xml-dev@jdlh.com> wrote:
> Hello, XML-dev:
>
> I am writing an XSLT filter which reads an XML file, and
> generates a shorter XML file with some selected elements (and
> all their children) removed.
>
> So far, my filter gives me output which is valid, well-formed XML,
> but it has blank lines where the removed elements used to be. Formally, I
> think the text node before the removed element remains, causing the
> blank line. I would like to remove this blank line, but leave all other
> indentation as-is. How can I do this?
>
> (If you winning points on Stackoverflow, this question is at
> <http://stackoverflow.com/questions/35407361/how-can-my-xslt-filter-avoid-leaving-blank-lines-in-output-xml-when-deleting-ele>,
> and I will upvote good answer there. A reply on this list is also welcome.
> The best answer so far on Stack Overflow says, essentially, that with
> XSLT 1.0 I'm hooped, but with XSLT 3.0 there are nice ways to express
> this filter.)
>
> A simplified version of my XSLT filter is:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="1.0">
>   <xsl:output method="xml" omit-xml-declaration="no" indent="yes"
> encoding="utf-8" />
>
>   <xsl:template match="node() | @*">
>     <xsl:copy>
>             <xsl:apply-templates select="node() | @*" />
>     </xsl:copy>
>   </xsl:template>
>
>   <xsl:template match="root/maybe[remove]" />
>
> </xsl:stylesheet>
>
>
> A very simplified version of my input XML file is:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <root>
>       <maybe><keep /></maybe>
>    <maybe><remove/></maybe>
> </root>
>
> Yes, the indentation is non-standard. I'm trying to make the point
> that I want the filter to leave the indentation it finds, except for
> the elements it removes. This lets me confirm the result using conventional
> diff.
>
> The output I get now (using xsltproc from libxslt, on MacOS X 10.10):
>
> <?xml version="1.0" encoding="utf-8"?>
> <root>
>       <maybe><keep/></maybe>
>
> </root>
>
>
> The blank line between <keep/> and </root> is what I'm trying to eliminate.
>
> Now, elsewhere on [Stack Overflow], related questions "XSLT: how to prevent
> the XSLT code from generating redundant blank-space in output xml"
> <http://stackoverflow.com/questions/1899687/xslt-how-to-prevent-the-xslt-code-from-generating-redundant-blank-space-in-outp>
> and "Removing blank lines in XSLT"
> <http://stackoverflow.com/questions/3265244/removing-blank-lines-in-xslt>
> suggest adding xsl:strip-space
> <https://www.w3.org/TR/xslt#element-strip-space>
> to the XSLT filter:
>
>     <xsl:strip-space elements="*"/>
>
>
> When I try that, the output file no longer has the blank line, but it
> now has different indentation than the original:
>
> <?xml version="1.0" encoding="utf-8"?>
> <root>
>   <maybe>
>     <keep/>
>   </maybe>
> </root>
>
>
> (Note that <maybe> and <keep/> and </maybe> are now on separate lines,
> and indented differently.)
>
> So, is there an XSLT element which will remove the blank line along
> with the element, but leave the other indentation and line breaks
> intact?
>
> Also, my real files are from GnuCash <http://gnucash.org/> and have
> a much more complex structure. My real XSLT filter has a more
> complex match expression. Thus, an XSLT element which doesn't require me
> to repeat the match expression is preferred.
>
> <xsl:template match='gnc-v2//gnc:account[@version="2.0.0"]/act:slots/
>     slot[slot:key/text()="import-map-bayes"]/slot:value[@type="frame"]/
>     slot/slot:value[@type="frame"]/slot[starts-with(slot:key/text(),
>         "Assets, Business, CAD:"
>     )]'
> />
>
> I'm using XSLT 1.0 because that's what my tool supports. Does XSLT 2.0
> provide a better answer for this question?
>
> Thank you in advance for your help.
>
> --
>   --Jim DeLaHunt, jdlh@jdlh.com   http://blog.jdlh.com/ (and jdlh.com)
>     multilingual websites consultant, Vancouver BC, Canada.



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.


[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS