2017-03-18 2 views
1

Ich habe das folgende XML-BeispielXSLT-Transformation ausgewählte Elemente nicht mit Standard-Namespace arbeiten

<?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> 

Die Kriterien nur kopieren, auf den „item-name“ Attributwert ist entweder TShirt, Ball und Schläger (dh: Es gibt viel mehr Elemente, aber wir verwenden nur eine Whitelist. Die resultierende XML sollte wie seine

<?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> 

Die folgende XSL nicht

<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> 

funktioniert Wenn der Standard-Namensraum "urn: com: SSN: schema: Export: SSNExportFormat.xsd" von XML entfernt dann funktioniert das XSLT. Können Sie helfen?

Antwort

1

Wenn das Eingabedokument über den Standardnamespace verfügt, sollte das entsprechende Stylesheet es mit dem Namespacepräfix deklarieren.

Das modifizierte Sheet:

<?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> 

Das Ergebnis:

<?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> 
Verwandte Themen