Reputation: 571
My XML is like this:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Rowsets CachedTime="" DateCreated="2013-07-30T00:13:32" EndDate="2013-07-30T00:13:29" StartDate="2013-07-29T23:13:29" Version="12.2.4 Build(92)">
<Rowset>
<Columns>
<Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="-9" SourceColumn="Name"/>
<Column Description="Value" MaxRange="1" MinRange="0" Name="Value" SQLDataType="-9" SourceColumn="Value"/>
<Column Description="Min" MaxRange="1" MinRange="0" Name="Min" SQLDataType="2" SourceColumn="Min"/>
<Column Description="Max" MaxRange="1" MinRange="0" Name="Max" SQLDataType="2" SourceColumn="Max"/>
<Column Description="ObjectDisplay" MaxRange="1" MinRange="0" Name="ObjectDisplay" SQLDataType="12" SourceColumn="ObjectDisplay"/>
<Column Description="Datatype" MaxRange="1" MinRange="0" Name="Datatype" SQLDataType="-9" SourceColumn="Datatype"/>
</Columns>
<Row>
<Name>Tank</Name>
<Value>T111</Value>
<Min>1</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
<Row>
<Name>Material</Name>
<Value>111-Bulk</Value>
<Min>7</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
<Row>
<Name>Batch</Name>
<Value>1111111</Value>
<Min>7</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
</Rowset>
<Rowset>
<Columns>
<Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="-9" SourceColumn="Name"/>
<Column Description="Value" MaxRange="1" MinRange="0" Name="Value" SQLDataType="-9" SourceColumn="Value"/>
<Column Description="Min" MaxRange="1" MinRange="0" Name="Min" SQLDataType="2" SourceColumn="Min"/>
<Column Description="Max" MaxRange="1" MinRange="0" Name="Max" SQLDataType="2" SourceColumn="Max"/>
<Column Description="ObjectDisplay" MaxRange="1" MinRange="0" Name="ObjectDisplay" SQLDataType="12" SourceColumn="ObjectDisplay"/>
<Column Description="Datatype" MaxRange="1" MinRange="0" Name="Datatype" SQLDataType="-9" SourceColumn="Datatype"/>
</Columns>
<Row>
<Name>Tank</Name>
<Value>T222</Value>
<Min>1</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
<Row>
<Name>Material</Name>
<Value>222-Bulk</Value>
<Min>7</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
<Row>
<Name>Batch</Name>
<Value>2222222</Value>
<Min>7</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
</Rowset>
</Rowsets>
I use following XSLT to transform above mentioned XML:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<!-- <xsl:strip-space elements="*"/>-->
<xsl:template match="/*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<Rowset>
<Columns>
<xsl:apply-templates mode="cols" select="Rowset[1]/Row"/>
</Columns>
<xsl:apply-templates select="Rowset"/>
</Rowset>
</xsl:copy>
</xsl:template>
<xsl:template match="Row" mode="cols">
<Column Description="" MaxRange="1" MinRange="0" Name="{Name}" SQLDataType="1" SourceColumn="{Name}"/>
</xsl:template>
<xsl:template match="Rowset">
<Row>
<xsl:apply-templates select="Row/Name"/>
</Row>
</xsl:template>
<xsl:template match="Name">
<xsl:element name="{.}">
<xsl:value-of select="../Value"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
I get proper Out as following:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Rowsets CachedTime="" DateCreated="2013-07-30T00:13:32" EndDate="2013-07-30T00:13:29" StartDate="2013-07-29T23:13:29" Version="12.2.4 Build(92)">
<Rowset>
<Columns>
<Column Description="" MaxRange="1" MinRange="0" Name="Tank" SQLDataType="1" SourceColumn="Tank"/>
<Column Description="" MaxRange="1" MinRange="0" Name="Material" SQLDataType="1" SourceColumn="Material"/>
<Column Description="" MaxRange="1" MinRange="0" Name="Batch" SQLDataType="1" SourceColumn="Batch"/>
</Columns>
<Row>
<Tank>T111</Tank>
<Material>111-Bulk</Material>
<Batch>1111111</Batch>
</Row>
<Row>
<Tank>T222</Tank>
<Material>222-Bulk</Material>
<Batch>2222222</Batch>
</Row>
</Rowset>
</Rowsets>
But now when I have following XML and when I run XSLT, I gives me below mentioned error:
XML:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Rowsets CachedTime="" DateCreated="2013-07-30T00:13:32" EndDate="2013-07-30T00:13:29" StartDate="2013-07-29T23:13:29" Version="12.2.4 Build(92)">
<Rowset>
<Columns>
<Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="-9" SourceColumn="Name"/>
<Column Description="Value" MaxRange="1" MinRange="0" Name="Value" SQLDataType="-9" SourceColumn="Value"/>
<Column Description="Min" MaxRange="1" MinRange="0" Name="Min" SQLDataType="2" SourceColumn="Min"/>
<Column Description="Max" MaxRange="1" MinRange="0" Name="Max" SQLDataType="2" SourceColumn="Max"/>
<Column Description="ObjectDisplay" MaxRange="1" MinRange="0" Name="ObjectDisplay" SQLDataType="12" SourceColumn="ObjectDisplay"/>
<Column Description="Datatype" MaxRange="1" MinRange="0" Name="Datatype" SQLDataType="-9" SourceColumn="Datatype"/>
</Columns>
<Row>
<Name>Tank Tag</Name>
<Value>T111</Value>
<Min>1</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
<Row>
<Name>Material</Name>
<Value>111-Bulk</Value>
<Min>7</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
<Row>
<Name>Batch</Name>
<Value>1111111</Value>
<Min>7</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
</Rowset>
<Rowset>
<Columns>
<Column Description="Name" MaxRange="1" MinRange="0" Name="Name" SQLDataType="-9" SourceColumn="Name"/>
<Column Description="Value" MaxRange="1" MinRange="0" Name="Value" SQLDataType="-9" SourceColumn="Value"/>
<Column Description="Min" MaxRange="1" MinRange="0" Name="Min" SQLDataType="2" SourceColumn="Min"/>
<Column Description="Max" MaxRange="1" MinRange="0" Name="Max" SQLDataType="2" SourceColumn="Max"/>
<Column Description="ObjectDisplay" MaxRange="1" MinRange="0" Name="ObjectDisplay" SQLDataType="12" SourceColumn="ObjectDisplay"/>
<Column Description="Datatype" MaxRange="1" MinRange="0" Name="Datatype" SQLDataType="-9" SourceColumn="Datatype"/>
</Columns>
<Row>
<Name>Tank Tag</Name>
<Value>T222</Value>
<Min>1</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
<Row>
<Name>Material</Name>
<Value>222-Bulk</Value>
<Min>7</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
<Row>
<Name>Batch</Name>
<Value>2222222</Value>
<Min>7</Min>
<Max>20</Max>
<ObjectDisplay>Enabled</ObjectDisplay>
<Datatype>String</Datatype>
</Row>
</Rowset>
</Rowsets>
Error:
[ERROR] [XSLSingleTableTransformation]XSLTransform error: java.lang.RuntimeException: An attribute whose value must be a QName had the value 'Tank Tag'
How can I remove this error?
If this error is due to space in between tank and tag, then 1) Can I get XML tag name with spaces? 2) If not, how canI remove that space and instead as "-"
Upvotes: 1
Views: 2374
Reputation: 117100
You just need to change this:
<xsl:template match="Name">
<xsl:element name="{.}">
into this:
<xsl:template match="Name">
<xsl:element name="{translate(., ' ' , '_')}">
This will translate the spaces in the name into underscores. Note that there are other restrictions on what can be used as an element name, so this may not be the end of it.
Upvotes: 6
Reputation: 86774
Tag names cannot contain spaces. You can use the replace
function to substitute a hyphen for the space.
If you can use XSLT 2, then it is simple
<xsl:template match="Name">
<xsl:element name="{replace(.,' ','-')}">
<xsl:value-of select="../Value"/>
</xsl:element>
</xsl:template>
For XSLT 1 it's a little more complex, you have to use EXSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
...
<xsl:template match="Name">
<xsl:element name="{str:replace(.,' ','-')}">
<xsl:value-of select="../Value"/>
</xsl:element>
</xsl:template>
...
Upvotes: 4