Reputation: 6158
I want to transfer a xml
file based on xsd
using xsl
.but not able to transfer this properly as mentioned on xsd.
this is my xml.
<abc:abcTransactionTypeXml xmlns:abc="abcTransactionType.schema.xml.google.com">
<abc:id>4</abc:id>
<abc:abcTransactionTypeCategoryId>1</abc:abcTransactionTypeCategoryId>
<abc:description>Post ofccice</abc:description>
<abc:type>POST</abc:type>
</abc:abcTransactionTypeXml>
here is .xsd
<xs:element name="abcTransactionType" type="abcTransactionType" />
<xs:complexType name="abcTransactionType">
<xs:sequence>
few elements are here
</xs:sequence>
</xs:complexType>
<xs:complexType name="abcTransactionTypeCategory" >
<xs:sequence>
<xs:element name="id" type="xs:short" minOccurs="1" maxOccurs="1"/>
<xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="type" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
here is my .xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:abc="abcTransactionType.schema.xml.google.com"
exclude-result-prefixes="abc"
xmlns="abcTransactionType.schema.abc.xml.google.com"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:template match="/">
<abcTransactionType>
<xsl:apply-templates select="abc:abcTransactionTypeXml" />
</abcTransactionType>
</xsl:template>
<xsl:template match="abc:abcTransactionTypeXml">
<xsl:apply-templates select="abct:abcTransactionTypeCategoryId" />
<xsl:apply-templates select="abc:description" />
<xsl:apply-templates select="abc:type" />
</xsl:template>
<xsl:template match="abc:id">
<id>
<xsl:value-of select="." />
</id>
</xsl:template>
<xsl:template match="abc:abcTransactionTypeCategoryId">
<abcTransactionTypeCategory>
<id>
<xsl:value-of select="." />
</id>
</abcTransactionTypeCategory>
</xsl:template>
<xsl:template match="abc:description">
<abcTransactionTypeCategory>
<description>
<xsl:value-of select="." />
</description>
</abcTransactionTypeCategory>
</xsl:template>
<xsl:template match="abc:type">
<abcTransactionTypeCategory>
<type>
<xsl:value-of select="." />
</type>
</abcTransactionTypeCategory>
</xsl:template>
</xsl:stylesheet>
and here is my output
<abcTransactionType xmlns="abcTransactionType.schema.abc.xml.google.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<id>4</id>
<abcTransactionTypeCategory>
<id>1</id>
</abcTransactionTypeCategory>
<abcTransactionTypeCategory>
<description>POST OFFICE</description>
</abcTransactionTypeCategory>
<abcTransactionTypeCategory>
<type>POST</type>
</abcTransactionTypeCategory>
<
</abcTransactionType>
here is my expected out put
<abcTransactionType xmlns="abcTransactionType.schema.abc.xml.google.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<id>4</id>
<abcTransactionTypeCategory>
<id>1</id>
<description>Post Office</description>
<type>POST</type>
</abcTransactionTypeCategory>
</abcTransactionType>
I need id,description,type in a single object which is abcTransactionTypeCategory I tried a lot to get the expected out put but no luck, can any one help me to get this done. Thanks a ton.
Upvotes: 2
Views: 101
Reputation: 9627
For an first solution there are only small changes necessary.
Move the output for <abcTransactionTypeCategory>
into the abc:abcTransactionTypeXml
template.
<xsl:template match="abc:abcTransactionTypeXml">
<xsl:apply-templates select="abc:id" />
<abcTransactionTypeCategory>
<xsl:apply-templates select="abc:abcTransactionTypeCategoryId" />
<xsl:apply-templates select="abc:description" />
<xsl:apply-templates select="abc:type" />
</abcTransactionTypeCategory>
</xsl:template>
And then remove <abcTransactionTypeCategory>
from each other template.
Second improvement: Add a template which remove the namespace prefix abc
<xsl:template match="abc:*" >
<xsl:element name="{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
Now you can remove all templates which only remove the namespace prefix.
Therefore try this:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:ssat="abcTransactionType.schema.xml.google.com"
exclude-result-prefixes="abc"
xmlns="abcTransactionType.schema.abc.xml.google.com"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<abcTransactionType>
<xsl:apply-templates select="abc:abcTransactionTypeXml" />
</abcTransactionType>
</xsl:template>
<xsl:template match="abc:abcTransactionTypeXml">
<xsl:apply-templates select="abc:id" />
<abcTransactionTypeCategory>
<xsl:apply-templates select="abc:abcTransactionTypeCategoryId" />
<xsl:apply-templates select="abc:description" />
<xsl:apply-templates select="abc:type" />
</abcTransactionTypeCategory>
</xsl:template>
<xsl:template match="abc:id">
<id>
<xsl:value-of select="." />
</id>
</xsl:template>
<xsl:template match="abc:abcTransactionTypeCategoryId">
<id>
<xsl:value-of select="." />
</id>
</xsl:template>
<xsl:template match="abc:*" >
<xsl:element name="{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Which will generate the following output:
<?xml version="1.0"?>
<abcTransactionType xmlns="abcTransactionType.schema.abc.xml.google.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<id>4</id>
<abcTransactionTypeCategory>
<id>1</id>
<description>POST OFFICE</description>
<type>POST</type>
</abcTransactionTypeCategory>
</abcTransactionType>
Upvotes: 1