0

I have an xml

<?xml version="1.0"?>
    <ArrayOfSubscriber xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Subscriber>
        <FirstName xsi:nil="true" xmlns="somenamespace" />
        <LastName xsi:nil="true" xmlns="somenamespace" />
        ...
      </Subscriber>
...
</ArrayOfSubscriber>

I need to create an excel file where each column has its own style. I hoped to get what I expected using the next transformation

<Worksheet>
  <Table>
    <xsl:apply-templates select="//Subscriber" />
  </Table>
</Worksheet>

<xsl:template match="Subscriber">
  <Row ss:Height="15">
    <Cell ss:StyleID="s21">
      <Data ss:Type="String">
        <xsl:value-of select="FirstName" />
      </Data>
    </Cell>
    <Cell ss:StyleID="s22">
      <Data ss:Type="String">
        <xsl:value-of select="LastName " />
      </Data>
    </Cell>
    ...
   </Row>
</xsl:template>

But the problem appears because of namespaces xmlns="somenamespace". Thus I can't get the data. I found a few topics where the possibility of removing them was described but as far as I new to xslt I didn't succeed in applying the right template to my task.

Alex Kovanev
  • 1,858
  • 1
  • 16
  • 29
  • Look at the accepted response in this link http://stackoverflow.com/questions/284094/how-to-select-from-xml-with-namespaces-with-xslt – JustinJDavies Nov 20 '12 at 13:06

1 Answers1

1

Try the alternative in the post I linked to earlier using a construct similar to this one:

<xsl:value-of select="*[local-name()='FirstName' = and namespace-uri()='somenamespace']" />

Previously suggested to define the namespace a parent element:

<parent xmlns:sn="somenamespace">
...
<xsl:template match="Subscriber">
  <Row ss:Height="15">
    <Cell ss:StyleID="s21">
      <Data ss:Type="String">
        <xsl:value-of select="sn:FirstName" />
      </Data>
    </Cell>
    <Cell ss:StyleID="s22">
      <Data ss:Type="String">
        <xsl:value-of select="sn:LastName " />
      </Data>
    </Cell>
    ...
   </Row>
</xsl:template>
...
</parent>
JustinJDavies
  • 2,663
  • 4
  • 30
  • 52
  • If to define `sn` on the workbook's tag level you'll get an error. Will work if do this inside `xsl:stylesheet`. But the problem is I wouldn't like to put namespaces here. That will mean I should keep in my mind this while every changes in the namespaces. Is there another way? – Alex Kovanev Nov 20 '12 at 14:11
  • It's probably a good idea to declare the namespaces at the highest level that you can as this will make your document more readable and less error-prone. Re-stating the same namespace declaration multiple times within the same document is generally a bad smell – JustinJDavies Nov 20 '12 at 16:35