| cycotron69 |
| Member |
| Anping Cai |
| US |
|
|
| None Specified |
|
| Wednesday, January 19, 2005 |
| Friday, October 30, 2009 7:35:55 PM |
6 [0.03% of all post / 0.00 posts per day] |
|
I've been using the following script to remove any elements without a value or contains only white-space from our XMLs.
Code: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:preserve-space elements="*"/> <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
<xsl:template match="*[not(normalize-space())]"/> </xsl:stylesheet>
This script will even remove empty elements with attributes. I however would like to keep those. So for example running the script on the following XML:
Code: <?xml version="1.0" encoding="UTF-8"?> <Root> <Parent1> <Child1 abc="xyz" >Me</Child1> </Parent1> <Parent2/> <Parent3> <Child3 abc="xyz"/> </Parent3> <Parent4> <Child4> </Child4> </Parent4> </Root>
Results in:
Code: <?xml version="1.0" encoding="UTF-8"?> <Root> <Parent1> <Child1 abc="xyz">Me</Child1> </Parent1> </Root>
But what I want is:
Code: <?xml version="1.0" encoding="UTF-8"?> <Root> <Parent1> <Child1 abc="xyz" >Me</Child1> </Parent1>
<Parent3> <Child3 abc="xyz"/> </Parent3> </Root>
I tried a couple of different modifications to the original script with no luck. Any help would be great. Many thanks in advance.
|
Thanks Martin, just confirmed with our admin that they are using XLST 1.0 engine, so looks like there's not much that can be done now, but the scripts is pretty cool, I'll save it for another day. Thanks for all the help!
|
Hi Martin,
so turn out the XSLT engine we are using doesn't fully support XSLT 2.0, so I'm not able to use "castable". I've been playing around with using regex to do match & replace on the dates, but I have very little experience with using regex with XSLT. Below is the regex match and replace expression I came up with, any idea on how to get it to work in an XSLT script? This would work for XSLT 1.0 too then right?
Match Expression: (^\-?\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:)(\d{2})((\.\d+)?(Z|([\+\-]{1}\d{2}:\d{2}))?$) Replace Expression: $100$3
Thanks for your help.
Mike
|
Sweet! Works like a charm. Thanks very much!
|
Hi Martin, thanks for your response, that was great. It works mostly, but I ran into a little problem when I use a XML with a node like below. Seems the script will collapse all of the child nodes if the nested construct contains only a single dateTime element.
Code: <Root>
<withOutString> <oneDateElement> <time>2009-09-04T09:12:38</time> </oneDateElement> </withOutString>
<withString> <string>OK</string> <time>2009-09-04T09:12:38</time> </withString>
<multipleDates> <time>2009-09-04T09:12:38</time> <time>2009-09-04T09:12:38</time> </multipleDates>
</Root>
and the result I got was
Code: <Root>
<withOutString>2009-09-04T09:12:00</withOutString>
<withString> <string>OK</string> <time>2009-09-04T09:12:00</time> </withString>
<multipleDates> <time>2009-09-04T09:12:00</time> <time>2009-09-04T09:12:00</time> </multipleDates>
</Root>
|
Hi,
I want to change all of the xs:dateTime values in an XML to have "00" for seconds using XSLT. For example:
Code: <Root> <timeStamp>2009-10-16T13:08:47Z</timeStamp> <schedule> <item> <name>Name1</name> <dateTime>2009-10-16T20:05:30Z</dateTime> </item> <item> <name>Name2</name> <dateTime>2009-10-16T122:20:15Z</datetime> </item> </schedule> </Root>
should be transformed into:
Code: <Root> <timeStamp>2009-10-16T13:08:00Z</timeStamp> <schedule> <item> <name>Name1</name> <dateTime>2009-10-16T20:05:00Z</dateTime> </item> <item> <name>Name2</name> <dateTime>2009-10-16T22:20:00Z</datetime> </item> </schedule> </Root>
I've been struggling with this one and can't think of a simple way of doing this beside looping through each element and do a string edit. Would it be possible to use a template that can be applied to all xs:dateTime fields?
thanks,
Mike
|
|