0

Environment: Java:8,Saxon-HE:9.9.0-2

My case is as I think trivial, however, I can not get a satisfying result

Source xml look like this:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="mcf.xsl"?>
<mcf xmlns="http://example.com/#mcf"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://example.com/#mcf">
    ...

I wrote xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0" >
<xsl:output method="text" omit-xml-declaration="yes" indent="yes" media-type="text/csv" exclude-result-prefixes="#all" escape-uri-attributes="yes"/>
     <xsl:template match="/" name="initial">
    <xsl:variable name="md" select="./mcf/md"/>
    <xsl:for-each select="$md/mi/mv">
        <xsl:variable name="mii" select="substring-after(../@mii,'pg=')"/>
        <xsl:variable name="ji" select="../job/@jobId"/>
        <xsl:variable name="gpd" select="../gp/@d"/>
        <xsl:variable name="gpet" select="../gp/@et"/>
        <xsl:call-template name="mv">
            <xsl:with-param name="mii" select="$mii"/>
            <xsl:with-param name="ji" select="$ji"/>
            <xsl:with-param name="gpd" select="$gpd"/>
            <xsl:with-param name="gpet" select="$gpet"/>
        </xsl:call-template>
        <xsl:value-of select="$newline"/>
    </xsl:for-each>
</xsl:template>

Unfortunately, the result is empty. If I remove the atrybut from "mcf" everything works fine.

I try use:

  <xsl:copy copy-namespaces="no" inherit-namespaces="no">

however, no results.

3Qn
  • 143
  • 1
  • 6
  • 22

1 Answers1

1

A namespace declaration is not an attribute.

Since your source XML has a default namespace, you must tell your stylesheet to use it:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xpath-default-namespace="http://example.com/#mcf"
exclude-result-prefixes="xs">
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • It could not be that simple: D – 3Qn Jan 17 '19 at 22:44
  • 1
    It's not always that simple: https://www.jenitennison.com/2007/07/01/the-perils-of-default-namespaces.html – michael.hor257k Jan 17 '19 at 22:47
  • I spent three days on it, using the Apache transformer everything worked but the performance did not give me peace. I rewrote the XSL changed the library to Saxona works faster but I could not solve this problem. Thank you very much. – 3Qn Jan 17 '19 at 22:51
  • 1
    You would (or at least should) have the same problem with any XSLT processor. However, the solution above can only work if the processor supports XSLT 2.0. In XSLT 1.0 you would have to use a prefix: https://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node/34762628#34762628 – michael.hor257k Jan 17 '19 at 23:19