0

I have a xml generated from SharePoint and I need to extract some data and generate a simplifed xml

This is my original (simplified) source file

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="http://itkm.gamesacorp.com/applications/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
    <id>f8a19240-e319-454a-b4a6-fab8e3470c8d</id>
    <title />
    <updated>2018-01-17T15:13:15Z</updated>
    <entry m:etag="&quot;53&quot;">
        <id>Web/Lists(guid'fe244c05-99f9-4b69-8727-46327122b245')/Items(1)</id>
        <content type="application/xml">
            <m:properties>
                <d:Codigo>0002</d:Codigo>
                <d:App_x0020_name>GOT</d:App_x0020_name>
            </m:properties>
        </content>
    </entry>
    <entry m:etag="&quot;49&quot;">
        <id>Web/Lists(guid'fe244c05-99f9-4b69-8727-46327122b245')/Items(3)</id>
        <content type="application/xml">
            <m:properties>
                <d:Codigo>0006</d:Codigo>
                <d:App_x0020_name>ALTAIR</d:App_x0020_name>
            </m:properties>
        </content>
    </entry>
</feed>

And this is my xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/>

<xsl:template match="feed">
  <Applications>
      <xsl:for-each select="entry">
         <App>
             <Code><xsl:value-of select="content/properties/Codigo"/></Code>
             <Name><xsl:value-of select="content/properties/App_x0020_name"/></Name>
             <Uri><xsl:value-of select="id"/></Uri>
        </App>
      </xsl:for-each>
  </Applications>
</xsl:template>
</xsl:stylesheet>

If I omit namespaces in both xml and xslt, the result is as expected, but I need the xlst working with the namespaces, but I don't what namespaces (and how) include in the xslt

This is the modified xml that works for me, please note that I removed all the references to the namespaces:

<?xml version="1.0" encoding="utf-8"?>
<feed>
    <id>f8a19240-e319-454a-b4a6-fab8e3470c8d</id>
    <title />
    <updated>2018-01-17T15:13:15Z</updated>
    <entry etag="&quot;53&quot;">
        <id>Web/Lists(guid'fe244c05-99f9-4b69-8727-46327122b245')/Items(1)</id>
        <content type="application/xml">
            <m:properties>
                <Codigo>0002</Codigo>
                <App_x0020_name>GOT</App_x0020_name>
            </m:properties>
        </content>
    </entry>
</feed>

How can I do with the xslt to work with namespaces?

mnieto
  • 3,744
  • 4
  • 21
  • 37

1 Answers1

1

You need to declare the namespaces in your XSLT, and then use the relevant namespace prefixes in all element names in your xpath expressions

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:a="http://www.w3.org/2005/Atom" 
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
    version="1.0"
    exclude-result-prefixes="a d m">
<xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="no"/>

<xsl:template match="a:feed">
  <Applications>
      <xsl:for-each select="a:entry">
         <App>
             <Code><xsl:value-of select="a:content/m:properties/d:Codigo"/></Code>
             <Name><xsl:value-of select="a:content/m:properties/d:App_x0020_name"/></Name>
             <Uri><xsl:value-of select="a:id"/></Uri>
        </App>
      </xsl:for-each>
  </Applications>
</xsl:template>
</xsl:stylesheet>

Note that in your XML you have a default namespace (with no prefix), but in the XSLT I have assigned it to a prefix of a so that it can be used in the xpath expression. The prefixes use don't need to match the XML at all. It is the namespace URI that has to match.

Tim C
  • 70,053
  • 14
  • 74
  • 93