Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Friday, September 13, 2013

XSL: Copy All Elements With Required Elements

This is an example of taking an XML document and passing it through an XSL transformation with a few key points.

  1. It makes sure that some elements not in the original XML will show up in the resulting XML
  2. It makes sure all elements in the original XML show up in the transformed XML
  3. It changes the root element
  4. It modifies an element tag and adds some text to it
So here is the original XML:

Here is the transformation file:

And here are the results:

It's also easy to verify and play around with at freeformatter.com.

Notepad++: Use a Regex Match In The Replace Text

It seems like I'm always trying to remember how to use a regex match in the replaced text in Notepad++ so this time I'm writing it down.

First of all let's say we have some XML text
<app_score><xsl:value-of select="app_score" /></app_score>
<tr_weight><xsl:value-of select="tr_weight" /></tr_weight>
<rearend><xsl:value-of select="rearend" /></rearend>

And, now lets say we want to use the element tag to transform it to something like:
<xsl:template match="app_score" />
<xsl:template match="tr_weight" />
<xsl:template match="rearend" />


You can see the match attribute comes from the element name so here is how you can do that.

1) First we build our regex with matching...now for matching we surround the piece we want to match with parenthesis() and because we want to transform the entire line we need the regex to match the whole line. This is what I came up with for this example: <([^\s]+)>(.*)</\1>

There are a couple cool things to note here. First, we actually are matching two groups: inside the element([^\s]+) and the element's text(.*). Second, we're using the first match for the closing tag(</\1>).

2) Now we know how the matching works it's easy to insert the match into our replace text, like so: <xsl:template match="\1" /> where the \1 is the first matching group. We could just as easily use the \2 for the second matching group.


Tuesday, August 20, 2013

Create XML With Only XPath and Values

Today I found myself needing to create a pretty straight forward XML document. The catch was I was taking values from a database that included the xpath address and a value, and from there I had to create the document. It's not incredibly complicated but briefly googling it didn't turn up any results so here is my solution, using the dom4j (1.6.1) API. (Other dependencies include Jaxen, Apache Commons, and log4j).


The real meat of this solution is the addElementToParent method. What it does it searches for the parent element of the provided xpath. If it doesn't find the parent in the document it will create it via a recursive call. Another trick is it will create any siblings that are needed in case the parent has an index (like /root/parent[3]). Once all the prerequisite elements are created it will then create the element we originally asked for.

This example will create this XML even though we only really tell it to create 5 elements. (the elements depend on the data element so it is created automatically.