2017-03-18 4 views
1

I持って、次のXMLサンプルXSLT変換の選択要素はデフォルトの名前空間では動作しません

<?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> 
基準は唯一のコピーにある

ここで、「項目名」属性値のいずれかのTシャツ、ボールとバット(すなわち:さらに多くの要素がありますが、ホワイトリストのみを使用しています)。 ":COM:SSN:スキーマ:輸出:SSNExportFormat.xsdは骨壷" XMLから削除されたXMLは、次のXSLは、デフォルトの名前空間場合

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

動作しない

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

ようにする必要がありますXSLTが機能します。あなたは助けることができますか?

答えて

1

入力ドキュメントにデフォルトの名前空間がある場合、関連するスタイルシートは名前空間接頭辞を使用してそれを宣言する必要があります。

修飾スタイルシート:

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

結果:

<?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> 
関連する問題