SeaHam20
SeaHam20

Reputation: 13

XSL Transformation of XML into smaller XML

I have some XML files to convert into smaller standardized XML Files. For this I'm using VBA and XSLT as the template.

My issue is that I receive an error on the conversion and when using http://xsltransform.net/ I'm not able to catch the error in the XSLT template.

Can someone help me adjusting the XSLT to be able to extract/create a XML with the node "FileVersion"

Below a sample of the XML file:

    <?xml version="1.0" encoding="UTF-8"?>
<AuditFile xmlns="urn:UP:Standard" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:UP:Standard C:\Standard.XSD">
<Header>
<FileVersion>1_01</FileVersion>
<CompanyID>507</CompanyID>
<TaxBasis>W239</TaxBasis>
    
</Header>
</AuditFile>

The XSL Template used:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0" >


  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/Header">
    <root>
      <xsl:apply-templates select="FileVersion"/>
    </root>
  </xsl:template>

  <xsl:template match="Header">
    <FileVersion>
      <xsl:value-of select="FileVersion" />
    </FileVersion>
  </xsl:template>

</xsl:stylesheet>

Upvotes: 1

Views: 77

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66783

  • Your document content is bound to the namespace urn:UP:Standard. In order to address them in the XPath in your XSLT, you need to declare that namespace and use the prefix in your XPath. In this solution, I chose the namespace-prefix UP, but you can use whatever you would like as long as you are consistent in how you reference it in your XPath in the stylesheet.
  • The root element is /UP:AuditFile, so the template you have defined matching /Header won't match anything. You could either match on / or /UP:AuditFile or UP:AuditFile if you wanted to have a template that matched at the start of the document processing to create the <root> element and then generate the <FileVersion>.
  • From the template matching UP:AuditFile you want to apply-templates to it's UP:Header child element, which will then match on the template matching UP:Header, in which you can construct your own <FileVersion> element (without a namespace) and then select the xsl:value-of it's child element UP:FileVersion
  • Since the urn:UP:Standard namespace is not used in the output, you can exclude it from the output by adding exclude-result-prefixes="UP" to the xsl:stylesheet element.

Adjustments applied:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0" 
    xmlns:UP="urn:UP:Standard" exclude-result-prefixes="UP">

    <xsl:output method="xml" indent="yes" />
    
    <xsl:template match="UP:AuditFile">
        <root>
            <xsl:apply-templates select="UP:Header"/>
        </root>
    </xsl:template>
    
    <xsl:template match="UP:Header">
        <FileVersion>
            <xsl:value-of select="UP:FileVersion" />
        </FileVersion>
    </xsl:template>
    
</xsl:stylesheet>

Upvotes: 1

Related Questions