IMPORTANT:
this is not a Support Forum! Experienced users might answer from time to time questions posted here. If you need a professional and reliable answer, or if you want to report a bug, please contact Altova Support instead.

Profile: cycotron69
About
User Name: cycotron69
Forum Rank: Member
Real Name: Anping Cai
Location US
Occupation:
Interests:
Gender: None Specified
Statistics
Joined: Wednesday, January 19, 2005
Last Visit: Friday, October 30, 2009 7:35:55 PM
Number of Posts: 6
[0.03% of all post / 0.00 posts per day]
Avatar
Last 10 Posts
Topic: Stripping only empty elements without attributes
Posted: Friday, October 30, 2009 7:35:55 PM
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.

Topic: xs:dateTime manipulation using XSLT?
Posted: Friday, October 30, 2009 7:08:26 PM
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!

Topic: xs:dateTime manipulation using XSLT?
Posted: Friday, October 23, 2009 3:56:27 PM
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

Topic: xs:dateTime manipulation using XSLT?
Posted: Wednesday, October 21, 2009 5:54:12 PM
Sweet! Works like a charm. Thanks very much!

Topic: xs:dateTime manipulation using XSLT?
Posted: Wednesday, October 21, 2009 3:41:48 PM
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>




Topic: xs:dateTime manipulation using XSLT?
Posted: Tuesday, October 20, 2009 6:42:05 PM
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

Use of the Altova User Forum(s) is governed by the Altova Terms of Use.