?

Log in

No account? Create an account
XML and XSL help - A Suburbs Boy Living a Country Life [My Flickr Photos]
November 9th, 2005
09:15 pm

[Link]

Previous Entry Share Next Entry
XML and XSL help

Holy smokes!!! puzzledance's "sweety" M to the rescue!!! YAY!!! His comment is screened, because he put his e-mail in it, but I'll post the good XSL in a minute...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/">
    <Wrapper xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\My Documents\Altova Projects\Indicators.xsd">
      <xsl:for-each select="//A">		
        <xsl:choose>
          <xsl:when test="./ValueRedIndicator">
            <xsl:copy-of select="."/>
          </xsl:when>
          <xsl:otherwise>
            <A>
              <ValueRedIndicator>
                <xsl:if test="./B/C/Value='Red'">Yes</xsl:if>
                <xsl:if test="self::node()[not(self::node()/B/C/Value='Red')]">No</xsl:if>
              </ValueRedIndicator>
              <xsl:for-each select="./child::*">
                <xsl:copy-of select="."/>
              </xsl:for-each>
            </A>
          </xsl:otherwise>
        </xsl:choose>  
      </xsl:for-each>
    </Wrapper>
  </xsl:template>
</xsl:stylesheet>
Dear LJ friends...if you know XSL, you can save me from being paged at four-something this morning when schema validation fails because somebody didn't get an ICD change to our developers in time.

The example below is not the exact file, but it shows my problem pretty well.  There are four A elements in the file.  Two have a ValueRedIndicator; them we can leave alone and just pass on to the output file.  Two do not.  For those two, I must search the C elements and if there is a Value attribute with Value "Red," I must add a <ValueRedIndicator>Yes</ValueRedIndicator> to my A element; otherwise, I must add a <ValueRedIndicator>No</ValueRedIndicator>.  After adding my value, I have to spit back the amended A, including it's B child and C descendants, as they were.

In other words:  all I want to do is echo out my original XML, but add the ValueRedIndicator where needed.

HELP!!!

This seemed like a simple application of XSL/XPath, so I set out to learn it this morning.  Here I am ten hours later, still flailing...

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2006 sp1 U (http://www.altova.com) by Pete Thomas (HPTi) -->
<Wrapper xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\My Documents\Altova Projects\Indicators.xsd">
 <A>
  <ValueRedIndicator>Yes</ValueRedIndicator>
  <B>
   <C>
    <Value>Red</Value>
   </C>
   <C>
    <Value>Green</Value>
   </C>
  </B>
 </A>
 <A>
  <ValueRedIndicator>No</ValueRedIndicator>
  <B>
   <C>
    <Value>Green</Value>
   </C>
   <C>
    <Value>Blue</Value>
   </C>
  </B>
 </A>
 <A>
  <B>
   <C>
    <Value>Blue</Value>
   </C>
   <C>
    <Value>Yellow</Value>
   </C>
  </B>
 </A>
 <A>
  <B>
   <C>
    <Value>Yellow</Value>
   </C>
   <C>
    <Value>Red</Value>
   </C>
  </B>
 </A>
</Wrapper>

Tags:

(5 comments | Leave a comment)

Comments
 
From:fornorald
Date:November 10th, 2005 02:34 am (UTC)
(Link)
Wish I could help, but I've never really "got" XML. Even when I had to write programs to make use of someone else's XML output, it was just painpainpain. XML is icky.
[User Picture]
From:elaine_brennan
Date:November 10th, 2005 04:44 am (UTC)
(Link)
your basic XML geek here ...

Lemne take a look at this in the morning (at the moment, my brain is not interested in parsing angle brackets). You're correct that this is, in fact, a fairly trivial problem.
[User Picture]
From:unix_vicky
Date:November 10th, 2005 06:10 am (UTC)

The rest is left as an exercise...

(Link)
I thought the usual way to parse XML was to build a tree structure. Starting at the root, which could be "the document" as a whole, or in your case "Wrapper". Under that, you have four child nodes, each of the A's. You then pass everything between "" and "" to the recursive XML parser, which gives you the child nodes of each A, which I guess is B's. At some point, you've parsed the whole tree. You can then take each the A nodes, and see which have "Value" children, or "ValueRedIndicator" children. And insert "ValueRedIndicator" at the appropriate points in the tree where needed.

I think somewhere I have some old Java code that parsed HTML/XML (it was fairly dumb, and wanted matched tag pairs, so it makes a much better XML parser than HTML parse, although it didn't (yet) handle tags of the form . But you should be able to come up with that yourself (or find a library that does this fairly common job).

[User Picture]
From:unix_vicky
Date:November 10th, 2005 06:14 am (UTC)

Re: The rest is left as an exercise...

(Link)
Forgot to escape my tags. The "You then pass everything between '' and '' to the recursive XML parser," should have been '<A>' and '</A>' of course.

And "it didn't (yet) handle tags of the form ." should have been of the form <TAG/>.

[User Picture]
From:unix_vicky
Date:November 10th, 2005 06:34 am (UTC)

Re: The rest is left as an exercise...

(Link)
Oh, and I'll be on AIM all night (although only awake for another 30 minutes) if you haven't solved this already and want to discuss some more.
Powered by LiveJournal.com