Peter 'Happy' Thomas (happypete) wrote,
Peter 'Happy' Thomas
happypete

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: geekery
Subscribe

  • Farewell

    photognome died of heart failure yesterday. I don't have words right now.

  • My tweets

    Tue, 11:00: Pulling a full 250 KW and over 1,000 miles per hour charge rate at the new Reston teslamotors #SuperCharger. @ Wieh……

  • My tweets

    Fri, 19:57: Mixed meat meatballs, sauteed vegetables, loaded mashed potatoes with da_valentine and friends @ Corolla, North Car……

  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 5 comments