RSS

crimulus’ weekly Twitter updates — 2012-04-15

  • .RT @TheOnion: Rod Stewart Mistaken For Elderly Aunt http://t.co/CCLPMPMr #
  • .RT @cnnbrk: Bubba Watson won the #Masters Tournament in sudden death playoff against Louis Oosthuizen. http://t.co/6M3xrmni #
  • This Exists: ABC News Report On Teenage Girls Who Have Been 'Trained To Fight Demons' http://t.co/J2hkaPLg #
  • @SEIU @FamiliesUSA Seriously? That's a lot of dough, but even split over only 20 million citizens it's a whopping $100 a piece #
  • Just watched Wait Until Dark and via Wikipedia I think Efrem Zimbalist Jr. looks like David Duchovny http://t.co/cGft1IfB. #
  • @ewg118 What … can't wait for some wacky surprises to scratch your head about? lol #
  • Is This a Parrot on Mars? : Discovery News http://t.co/rhUVCHkI #
  • Scientists Establish First Working Quantum Network, Quantum Internet On The Way http://t.co/yYC06vq1 via @geekosystem I just drooled a bit. #
  • Remember the next time you hold a rabbit's foot for good luck, it did not do much good for the rabbit. #
  • RT @washingtonpost: 2012 has 3 Friday the 13ths and they are 13 weeks apart: http://t.co/zNNsTOKf . #
  • @historytweeter There were more Civil War soldiers that died than soldiers that thought ?!?! 🙂 #

Powered by Twitter Tools

 

Posted by on April 15, 2012 in Twitter Updates

Leave a comment

Tags: , ,

XSL Template for converting XSD to complete XML prototype with inline specifications

For a project relating to Amazon, I found a need for an XSL template that could convert their XSD files–which are both numerous and meaty–into complete XML prototypes.  This would be useful for two reasons.

First, it would generate a complete sample call for basically any API request for which the provide an XSD.  Second, it would give me XML that I could programmatically convert to a self-generating form if necessary.

After digging around the web, to my surprise I was unable to find an XSL already made that accomplishes this, so I set about making my own.

The resulting XSL is included below.

Please note that while this appears (as of this writing) to be 100% functional for what I need on Amazon, it does not cover all possible XSD configurations, so may need to be modified for more exotic XSDs.  Also, it’s possible (and quite likely) that the XML generated will not validate to the given XSD. This is because I had to find ways to pass value restriction meta data in a way that made sense in XML.

The two obvious points are nodes with enumerations in which the valid values are passed as <Value> subnodes and nodes with extendable attributes that had to be constructed in the way of other nodes.  In both cases, a flag is set as an attribute in the parent node.

If you make enhancements or fixes or if you find problems, feel free to let me know, and I will revise this.

The XSL is free for anyone to use, modify, etc. without restraint, but I’d certainly be happy to hear about it if it helps you out.

Here is the code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0"
>

<xsl:output method=”xml”/>

<!– Ignore nodes that may have text values as they will output directly otherwise –>
<xsl:template match=”*[not(xs:element) and not(xs:include)]” />

<!– Uncomment to process any included files inline–>
<!–
<xsl:template match=”//xs:include”>
<xsl:for-each select=”document(@schemaLocation)/xs:schema/xs:element”>
<xsl:call-template name=”element” />
</xsl:for-each>
</xsl:template>
–>

<!– ######################
## XML ELEMENTS TEMPLATE ##
####################### –>

<xsl:template match=”/xs:schema/xs:element” name=”element”>

<xsl:choose>

<!– Node is referencing a base element – use that for the prototype either from this file or from any included files–>
<xsl:when test=”@ref”>
<xsl:variable name=”elementRef” select=”current()/@ref” />
<xsl:for-each select=”//xs:element[@name=$elementRef]”><xsl:call-template name=”element”/></xsl:for-each>
<xsl:for-each select=”//xs:include”>
<xsl:for-each select=”document(@schemaLocation)//xs:element[@name=$elementRef]”><xsl:call-template name=”element”/></xsl:for-each>
</xsl:for-each>
</xsl:when>

<!– The node is a standard element – output its XML Prototype –>
<xsl:otherwise>

<xsl:element name=”{@name}”>

<!– Carry all attributes through other than name (which is used for the element nodeName) and type (which we have to handle cases for) –>
<xsl:copy-of select=”@*[local-name() != ‘name’ and local-name() != ‘type’]” />

<!– Bring in any annotations for the element as a “note” attribute –>
<xsl:if test=”./xs:annotation/xs:documentation”>
<xsl:attribute name=”note”><xsl:value-of select=”./xs:annotation/xs:documentation” /></xsl:attribute>
</xsl:if>

<xsl:choose>

<!– If there is a type attribute, it implies that it’s a complexType node and so we’ll be handling its subNodes independently –>
<xsl:when test=”contains(@type,’:’)”>
<xsl:variable name=”elementType” select=”substring-after(@type,’:’)” />
<xsl:for-each select=”//xs:complexType[@name=$elementType]”><xsl:call-template name=”complexType”/></xsl:for-each>
<xsl:for-each select=”//xs:simpleType[@name=$elementType]”><xsl:call-template name=”simpleType”/></xsl:for-each>
<xsl:for-each select=”//xs:include”>
<xsl:for-each select=”document(@schemaLocation)//xs:complexType[@name=$elementType]”><xsl:call-template name=”complexType”/></xsl:for-each>
<xsl:for-each select=”document(@schemaLocation)//xs:simpleType[@name=$elementType]”><xsl:call-template name=”simpleType”/></xsl:for-each>
</xsl:for-each>
</xsl:when>
<xsl:when test=”@type”>
<xsl:variable name=”elementType” select=”@type” />
<xsl:for-each select=”//xs:complexType[@name=$elementType]”><xsl:call-template name=”complexType”/></xsl:for-each>
<xsl:for-each select=”//xs:simpleType[@name=$elementType]”><xsl:call-template name=”simpleType”/></xsl:for-each>
<xsl:for-each select=”//xs:include”>
<xsl:for-each select=”document(@schemaLocation)//xs:complexType[@name=$elementType]”><xsl:call-template name=”complexType”/></xsl:for-each>
<xsl:for-each select=”document(@schemaLocation)//xs:simpleType[@name=$elementType]”><xsl:call-template name=”simpleType”/></xsl:for-each>
</xsl:for-each>
</xsl:when>

<!– This is a direct element definition but it still may have inline definitions –>
<xsl:otherwise>
<xsl:for-each select=”./xs:complexType”><xsl:call-template name=”complexType”/></xsl:for-each>
<xsl:for-each select=”./xs:simpleType”><xsl:call-template name=”simpleType”/></xsl:for-each>
<xsl:for-each select=”./xs:element”><xsl:call-template name=”element”/></xsl:for-each>
</xsl:otherwise>

</xsl:choose>

</xsl:element>

</xsl:otherwise>

</xsl:choose>

</xsl:template>

<!– ######################
## COMPLEX TYPE TEMPLATE ##
####################### –>

<xsl:template name=”complexType”>

<!– Pass along all attributes –>
<xsl:copy-of select=”@*[local-name() != ‘name’]” />

<!– If this complexType needs additional attributes, pass a way to use those –>
<xsl:if test=”./xs:simpleContent/xs:extension/xs:attribute”>

<!– Custom node to indicate that this is node has extended attributes –>
<xsl:attribute name=’isExtended’>1</xsl:attribute>

<!– Element is complexType because it’s an extends the attribute set of a simpleType –>
<xsl:if test=”./xs:simpleContent/xs:extension/@base”>
<xsl:choose>
<xsl:when test=”contains(./xs:simpleContent/xs:extension/@base,’:’)”>
<xsl:variable name=”baseSimpleType” select=”substring-after(./xs:simpleContent/xs:extension/@base,’:’)” />
<xsl:for-each select=”//xs:simpleType[@name=$baseSimpleType]”><xsl:call-template name=”simpleType”/></xsl:for-each>
<xsl:for-each select=”//xs:include”>
<xsl:for-each select=”document(@schemaLocation)//xs:simpleType[@name=$baseSimpleType]”><xsl:call-template name=”simpleType”/></xsl:for-each>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:variable name=”baseSimpleType” select=”./xs:simpleContent/xs:extension/@base” />
<xsl:for-each select=”//xs:simpleType[@name=$baseSimpleType]”><xsl:call-template name=”simpleType”/></xsl:for-each>
<xsl:for-each select=”//xs:include”>
<xsl:for-each select=”document(@schemaLocation)//xs:simpleType[@name=$baseSimpleType]”><xsl:call-template name=”simpleType”/></xsl:for-each>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:if>

<!– Pass along all other attributes (other than @base) –>
<xsl:copy-of select=”./xs:simpleContent/xs:extension/@*[local-name() != ‘base’]” />

<!– Give a container for the actual value of the node –>
<xsl:element name=”NodeValue” />

<!– Show all attributes as sub nodes –>
<xsl:for-each select=”./xs:simpleContent/xs:extension/xs:attribute”>
<xsl:element name=”{@name}”>
<xsl:variable name=”elementType” select=”current()/@type” />
<xsl:for-each select=”//xs:complexType[@name=$elementType]”><xsl:call-template name=”complexType”/></xsl:for-each>
<xsl:for-each select=”//xs:simpleType[@name=$elementType]”><xsl:call-template name=”simpleType”/></xsl:for-each>
<xsl:for-each select=”//xs:include”>
<xsl:for-each select=”document(@schemaLocation)//xs:complexType[@name=$elementType]”><xsl:call-template name=”complexType”/></xsl:for-each>
<xsl:for-each select=”document(@schemaLocation)//xs:simpleType[@name=$elementType]”><xsl:call-template name=”simpleType” /></xsl:for-each>
</xsl:for-each>
</xsl:element>
</xsl:for-each>

</xsl:if>

<!– Handle any sub nodes –>
<xsl:for-each select=”./xs:sequence/xs:element | ./xs:choice/xs:element”><xsl:call-template name=”element” /></xsl:for-each>

</xsl:template>

<!– #####################
## SIMPLE TYPE TEMPLATE ##
###################### –>

<xsl:template name=”simpleType”>

<!– If there is a base type, analyze and output its attributes first –>
<xsl:choose>
<xsl:when test=”contains(./xs:restriction/@base,’:’)”>
<xsl:variable name=”baseSimpleType” select=”substring-after(./xs:restriction/@base,’:’)” />
<xsl:attribute name='{$baseSimpleType}’>{type}</xsl:attribute>
<xsl:for-each select=”//xs:simpleType[@name=$baseSimpleType]”><xsl:call-template name=”simpleType” /></xsl:for-each>
<xsl:for-each select=”//xs:include”>
<xsl:for-each select=”document(@schemaLocation)//xs:simpleType[@name=$baseSimpleType]”><xsl:call-template name=”simpleType” /></xsl:for-each>
</xsl:for-each>
</xsl:when>
<xsl:when test=”./xs:restriction/@base”>
<xsl:variable name=”baseSimpleType” select=”./xs:restriction/@base” />
<xsl:attribute name='{$baseSimpleType}’>{type}</xsl:attribute>
<xsl:for-each select=”//xs:simpleType[@name=$baseSimpleType]”><xsl:call-template name=”simpleType” /></xsl:for-each>
<xsl:for-each select=”//xs:include”>
<xsl:for-each select=”document(@schemaLocation)//xs:simpleType[@name=$baseSimpleType]”><xsl:call-template name=”simpleType” /></xsl:for-each>
</xsl:for-each>
</xsl:when>
</xsl:choose>

<!– Pass all type attributes back –>
<xsl:copy-of select=”./@*[local-name() != ‘name’ and local-name() != ‘id’]” />

<!– Pass all non-enumeration nodes as attributes (minLength, etc.) –>
<xsl:for-each select=”./xs:restriction/*[local-name() != ‘enumeration’]”>
<xsl:attribute name=”{local-name()}”><xsl:value-of select=”@value” /></xsl:attribute>
</xsl:for-each>

<!– If enumerations exist, pass an attribute indicating so and pass the values as sub nodes (must come last because it outputs XML directly as text) –>
<xsl:for-each select=”./xs:restriction/xs:enumeration”>
<xsl:if test=”position() = 1″><xsl:attribute name=”isList”>1</xsl:attribute></xsl:if>
<xsl:element name=”Value”><xsl:value-of select=”current()/@value” /></xsl:element>
</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

 

Posted by on April 13, 2012 in Computers, How-To, Programming

4 Comments

Tags: , , , , , , , ,

crimulus’ weekly Twitter updates — 2012-04-08

  • @MudcatOakCliff @carolynporco Yet it is also people who are doing the science…methinks the argument has a FTL neutrino in it. #
  • @carolynporco Liability is required–not comp or collision. I don't think there is a way insure for people you have infected. #
  • @MudcatOakCliff Yeah it's easy to see things the way we want to, which is why it is dangerous to say anything at all. 🙂 #

Powered by Twitter Tools

 

Posted by on April 8, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-04-01

  • @carolynporco I share your love for Enceladus, but at the very least, Europa is a significantly closer target … #
  • @carolynporco I have no pref either way..I'd go and dig through the ice myself if I could. Just saying the media responds best to proximity. #

Powered by Twitter Tools

 

Posted by on April 1, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-03-25

  • Found it interesting that I can now pay directly with #PayPal at Home Depot checkout … #
  • So this half pound of chocolate goes into a bar … #

Powered by Twitter Tools

 

Posted by on March 25, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-03-18

Powered by Twitter Tools

 

Posted by on March 18, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-03-04

Powered by Twitter Tools

 

Posted by on March 4, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-02-19

Powered by Twitter Tools

 

Posted by on February 19, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-02-12

Powered by Twitter Tools

 

Posted by on February 12, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-01-29

Powered by Twitter Tools

 

Posted by on January 29, 2012 in Twitter Updates

Leave a comment

Tags: , ,