Friday, 15 May 2015

Unusual XSLT transformation - structuralization of XML -



Unusual XSLT transformation - structuralization of XML -

i having unusual problem. task "structuralize xml". input xml (example):

<documents> <document>review</document> <document_id>rev#1</document_id> <item>list of terms</item> <item_id>10</item_id> <item_description>terms used in documents</item_description> <item_attribute>term</item_attribute> <item_attribute_name>sa</item_attribute_name> <item_attribute_value>some attribute</item_attribute_value> <item_attribute_name>soa</item_attribute_name> <item_attribute_value>some other attribute</item_attribute_value> <document>interface</document> <document_id>ac-163</document_id> <item>list of terms</item> <item_id>15</item_id> <item_description>another item</item_description> <item_attribute>term</item_attribute> <item_attribute_name>name#1</item_attribute_name> <item_attribute_value>att#1</item_attribute_value> <item_attribute_name>name#2</item_attribute_name> <item_attribute_value>att#2</item_attribute_value> </documents>

what should transform next entity structure:

documents 1..* document 1..1 items 1..* item 1..1 attributes 1..* attribute

that means: element 'documents' may include many 'document', 'document' include 1 element named 'items', element 'items' may include many elements 'item', etc.

the expected output of above illustration is:

<documents> <document> <document_id>rev#1</document_id> <items> <item> <id>10</id> <description>terms used in documents</description> <attributes> <attribute> <name>sa</name> <value>some attribute</value> </attribute> <attribute> <name>soa</name> <value>some other attribute</value> </attribute> </attributes> </item> </items> </document> <document> <document_id>ac-163</document_id> <items> <item> <id>15</id> <description>another item</description> <attributes> <attribute> <name>name#1</name> <value>att#1</value> </attribute> <attribute> <name>name#2</name> <value>att#2</value> </attribute> </attributes> </item> </items> </document> </documents>

i need problem in task.... may kindly inquire help? unusual 'structuralize' xml - have ideas?

best regards!

this should pretty easy accomplish xslt grouping if you're using xslt 2.0, in case you're stuck xslt 1.0, here's xslt 1.0 compatible solution.

stylesheet <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" indent="yes"/> <!-- identity transform --> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="document"> <!-- store unique id of current element variable. --> <xsl:variable name="id" select="generate-id()"/> <xsl:copy> <xsl:apply-templates select="following-sibling::document_id[1]"/> <items> <!-- apply <item_attribute_name> elements first preceding <document> sibling element that's beingness processed. maintain processing *all* of rest of <item_attribute_name> elements in document. --> <xsl:apply-templates select="following-sibling::item [preceding-sibling::document[1][generate-id() = $id]]"/> </items> </xsl:copy> </xsl:template> <xsl:template match="item"> <xsl:variable name="id" select="generate-id()"/> <xsl:copy> <xsl:apply-templates select="following-sibling::item_id[1]"/> <xsl:apply-templates select="following-sibling::item_description[1]"/> <attributes> <xsl:apply-templates select="following-sibling::item_attribute_name [preceding-sibling::item[1][generate-id() = $id]]"/> </attributes> </xsl:copy> </xsl:template> <xsl:template match="documents"> <xsl:copy> <xsl:apply-templates select="document"/> </xsl:copy> </xsl:template> <xsl:template match="item_description"> <description> <xsl:apply-templates/> </description> </xsl:template> <xsl:template match="item_id"> <id> <xsl:apply-templates/> </id> </xsl:template> <xsl:template match="item_attribute_name"> <attribute> <name> <xsl:apply-templates/> </name> <xsl:apply-templates select="following-sibling::item_attribute_value[1]"/> </attribute> </xsl:template> <xsl:template match="item_attribute_value"> <value> <xsl:apply-templates/> </value> </xsl:template> <xsl:template match="item_attribute"/> </xsl:stylesheet> input <documents> <document>review</document> <document_id>rev#1</document_id> <item>list of terms</item> <item_id>10</item_id> <item_description>terms used in documents</item_description> <item_attribute>term</item_attribute> <item_attribute_name>sa</item_attribute_name> <item_attribute_value>some attribute</item_attribute_value> <item_attribute_name>soa</item_attribute_name> <item_attribute_value>some other attribute</item_attribute_value> <document>interface</document> <document_id>ac-163</document_id> <item>list of terms</item> <item_id>15</item_id> <item_description>another item</item_description> <item_attribute>term</item_attribute> <item_attribute_name>name#1</item_attribute_name> <item_attribute_value>att#1</item_attribute_value> <item_attribute_name>name#2</item_attribute_name> <item_attribute_value>att#2</item_attribute_value> </documents> output <?xml version="1.0"?> <documents> <document> <document_id>rev#1</document_id> <items> <item> <id>10</id> <description>terms used in documents</description> <attributes> <attribute> <name>sa</name> <value>some attribute</value> </attribute> <attribute> <name>soa</name> <value>some other attribute</value> </attribute> </attributes> </item> </items> </document> <document> <document_id>ac-163</document_id> <items> <item> <id>15</id> <description>another item</description> <attributes> <attribute> <name>name#1</name> <value>att#1</value> </attribute> <attribute> <name>name#2</name> <value>att#2</value> </attribute> </attributes> </item> </items> </document> </documents>

xml xslt transformation

No comments:

Post a Comment