1

I have the following XML sample

<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:com:ssn:schema:export:SSNExportFormat.xsd" Version="0.1" DocumentID="7bf15843-4fe7-4692-b638-b42ada6b86f6-2" ExportID="7bf15843-4fe7-4692-b638-b42ada6b86f6" JobID="132251" RunID="7238837" CreationTime="2017-03-09T04:00:08.209+13:00" StartTime="2017-03-08T01:00:00.000+13:00" EndTime="2017-03-09T01:00:00.000+13:00">
  <product product-id="clothes">
  <items>
    <item item-name="TShirt" size="L">Brown</item>
    <item item-name="Pants" size="L">Green</item>
    <item item-name="Shirt" size="L">White</item>
  </items>
</product>
<product product-id="toys">
  <items>
    <item item-name="Ball" size="L">Cyan</item>
    <item item-name="Bat" size="L">Green</item>
    <item item-name="Racket" size="L">Blue</item>
  </items>
  </product>
</catalog>

The criteria is to only copy where "item-name" attribute value is either TShirt, Ball and Bat (ie: there are many more elements but we only use a whitelist). THe resulting XML should be like

<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:com:ssn:schema:export:SSNExportFormat.xsd" Version="0.1" DocumentID="7bf15843-4fe7-4692-b638-b42ada6b86f6-2" ExportID="7bf15843-4fe7-4692-b638-b42ada6b86f6" JobID="132251" RunID="7238837" CreationTime="2017-03-09T04:00:08.209+13:00" StartTime="2017-03-08T01:00:00.000+13:00" EndTime="2017-03-09T01:00:00.000+13:00">
<product product-id="clothes">
  <items>
    <item item-name="TShirt" size="L">Brown</item>
  </items>
</product>
<product product-id="toys">
  <items>
    <item item-name="Ball" size="L">Cyan</item>
    <item item-name="Bat" size="L">Green</item>
  </items>
</product>
</catalog>

The following XSL does not work

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="urn:com:ssn:schema:export:SSNExportFormat.xsd"
                xmlns:local="local">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

    <local:WhiteList>
      <item-name>TShirt</item-name>
      <item-name>Pants</item-name>
      <item-name>Ball</item-name>
    </local:WhiteList>
    <xsl:variable name="whiteList" select="document('')/*/local:WhiteList/item-name"/>

    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="item">
      <xsl:if test="@item-name=$whiteList">
        <xsl:copy-of select="."/>
      </xsl:if>    
    </xsl:template>
  </xsl:stylesheet>

If the default namespace of "urn:com:ssn:schema:export:SSNExportFormat.xsd" is removed from XML then the XSLT works. Are you able to help?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Nathan
  • 21
  • 2

1 Answers1

1

If input document has default namespace, the relevant stylesheet should declare it using namespace prefix.

The modified stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ssne="urn:com:ssn:schema:export:SSNExportFormat.xsd"
    xmlns:local="local">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <local:WhiteList>
        <item-name>TShirt</item-name>
        <item-name>Pants</item-name>
        <item-name>Ball</item-name>
    </local:WhiteList>

    <xsl:variable name="whiteList" select="document('')/*/local:WhiteList/item-name"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ssne:item">
        <xsl:if test="@item-name = $whiteList">
            <xsl:copy-of select="."/>
        </xsl:if>    
    </xsl:template>
</xsl:stylesheet>

The result:

<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:com:ssn:schema:export:SSNExportFormat.xsd"
         Version="0.1"
         DocumentID="7bf15843-4fe7-4692-b638-b42ada6b86f6-2"
         ExportID="7bf15843-4fe7-4692-b638-b42ada6b86f6"
         JobID="132251"
         RunID="7238837"
         CreationTime="2017-03-09T04:00:08.209+13:00"
         StartTime="2017-03-08T01:00:00.000+13:00"
         EndTime="2017-03-09T01:00:00.000+13:00">
   <product product-id="clothes">
      <items>
         <item item-name="TShirt" size="L">Brown</item>
         <item item-name="Pants" size="L">Green</item>
      </items>
   </product>
   <product product-id="toys">
      <items>
         <item item-name="Ball" size="L">Cyan</item>
      </items>
   </product>
</catalog>
Toshihiko Makita
  • 1,294
  • 1
  • 8
  • 15