Reputation: 6595
I am doing a Schematron validation on an XML file and my result looks like this:
<fired-rule context="Message[@Name='SPY_IN']"/>
<failed-assert test="@OppositeMacAddress='0x00'" location="/Module[1]/Router[1]/Message[1]">
<text>!Error! Erwarteter Wert:"0x00"</text>
</failed-assert>
<fired-rule context="Configuration[@Address='W_ST_PLAMA_MOD_OWM_OPP']"/>
<failed-assert test="@Name='8'"
location="/Module[1]/DriverConfigurations[1]/DriverConfiguration[20]/Configuration[10]">
<text>!Error! Erwarteter Wert:"8"</text>
</failed-assert>
Now i am trying to generate a XML file that referes to the "location"-attribute. It should look like this:
<Module>
<Router>
<Message>
</Message>
</Router>
</Module>
<Module>
<DriverConfigurations>
<DriverConfiguration>
<Configuration>
</Configuration>
</DriverConfiguration>
</DriverConfigurations>
</Module>
I tried to refer to this: auto creating xml elements using XSLT But it somehow doesnt work for me. So i have no idea how my xsl file should look like. Any idea?
Upvotes: 0
Views: 100
Reputation: 101652
Give this a try:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<root>
<xsl:apply-templates select="//failed-assert/@location" />
</root>
</xsl:template>
<xsl:template match="@location">
<xsl:param name="path" select="." />
<xsl:variable name="currentContext"
select="substring-before(substring-after($path,'/'), '[')"/>
<xsl:variable name="subContext" select="substring-after($path, ']')"/>
<xsl:element name="{$currentContext}">
<xsl:apply-templates select="current()[string($subContext)]">
<xsl:with-param name="path" select="$subContext"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
When run on this input:
<root>
<fired-rule context="Message[@Name='SPY_IN']"/>
<failed-assert test="@OppositeMacAddress='0x00'" location="/Module[1]/Router[1]/Message[1]">
<text>!Error! Erwarteter Wert:"0x00"</text>
</failed-assert>
<fired-rule context="Configuration[@Address='W_ST_PLAMA_MOD_OWM_OPP']"/>
<failed-assert test="@Name='8'"
location="/Module[1]/DriverConfigurations[1]/DriverConfiguration[20]/Configuration[10]">
<text>!Error! Erwarteter Wert:"8"</text>
</failed-assert>
</root>
The result is:
<root>
<Module>
<Router>
<Message />
</Router>
</Module>
<Module>
<DriverConfigurations>
<DriverConfiguration>
<Configuration />
</DriverConfiguration>
</DriverConfigurations>
</Module>
</root>
Upvotes: 1