 |
 |
 |
Hello,
I am rather a beginner with xsl. What I am trying to do is to tranform the
input.xml file below with a xsl stylesheet to another xml-file.
input.xml:
<?xml version='1.0' encoding='UTF-8'?>
<TestReq xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<par1 xsi:length="20">par1</par1>
<par2>par2</par2>
</TestReq>
When I use the stylesheet st1.xml, I get the output outputst1.xml
st1.xml:
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance'>
<xsl:output method='xml' version='1.0' indent='yes' />
<xsl:variable name="stylesheet"
select="document('')/xsl:stylesheet" />
<xsl:template match='/'>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match='*'>
<xsl:element name='{name()}' namespace="http://www.xxx.com/yyy" >
<xsl:copy-of select="$stylesheet/namespace::xsi" />
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*[@xsi:length]">
<xsl:element name='withlength-{name()}'>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
outputst1.xml:
<?xml version="1.0" standalone="yes"?>
<TestReq xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns="http://www.xxx.com/yyy">
<withlength-par1 xmlns="">par1</withlength-par1>
<par2>par2</par2>
</TestReq>
You can see that the template match for the attribute xsi:length is
correctly matched (I figure because the version of xsi namespace in the
stylesheet is the same as that of the input file). When I use the stylesheet
st2.xml however, the template match for xsi:length is not handled at all
(apparently because the stylesheet does not have the same version of the xsi
namespace as the input: 2001 instead of 1999).
st2.xml:
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<xsl:output method='xml' version='1.0' indent='yes' />
<xsl:variable name="stylesheet"
select="document('')/xsl:stylesheet" />
<xsl:template match='/'>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match='*'>
<xsl:element name='{name()}' namespace="http://www.xxx.com/yyy" >
<xsl:copy-of select="$stylesheet/namespace::xsi" />
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*[@xsi:length]">
<xsl:element name='withlength-{name()}'>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
outputst2.xml:
<?xml version="1.0" standalone="yes"?>
<TestReq xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.xxx.com/yyy">
<par1>par1</par1>
<par2>par2</par2>
</TestReq>
I solved this by looking only at the local-name of the attribute, as shown
in stylesheet st3.xml
st3.xml:
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<xsl:output method='xml' version='1.0' indent='yes' />
<xsl:variable name="stylesheet"
select="document('')/xsl:stylesheet" />
<xsl:template match='/'>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match='*'>
<xsl:element name='{name()}' namespace="http://www.xxx.com/yyy" >
<xsl:copy-of select="$stylesheet/namespace::xsi" />
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*[@*[local-name()='length']]">
<xsl:element name='withlength-{name()}'>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
outputst3.xml
<?xml version="1.0" standalone="yes"?>
<TestReq xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.xxx.com/yyy">
<withlength-par1 xmlns="">par1</withlength-par1>
<par2>par2</par2>
</TestReq>
So my output is mainly correct, the only this is that an extra xmlns=""
appears in the tag withlength-par1. This can be removed by adding an extra
'namespace="http://www.xxx.com/yyy"' in the stylesheet as show below in
stylesheet st4.xml.
st4.xml:
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<xsl:output method='xml' version='1.0' indent='yes' />
<xsl:variable name="stylesheet"
select="document('')/xsl:stylesheet" />
<xsl:template match='/'>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match='*'>
<xsl:element name='{name()}' namespace="http://www.xxx.com/yyy" >
<xsl:copy-of select="$stylesheet/namespace::xsi" />
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*[@*[local-name()='length']]">
<xsl:element name='withlength-{name()}'
namespace="http://www.xxx.com/yyy">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Somehow I get the feeling that there should be an easier way to accomplish
this. Does anyone know how?
Kind regards,
Paul Vintges.
|
 | 

|  |
These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.
|  |
| |
 |
 |
 |