有一个xslt需要将元素从<old>
转换为DublinCore或Prism命名空间(即dc:XXX
或prism:XXX
)。我试图添加名称空间前缀但仍然没有运气。以下示例将“PMISSN”交换为“prism:issn”
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
xmlns:pam="http://prismstandard.org/namespaces/pam/2.2/"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:prism="http://prismstandard.org/namespaces/basic/2.2/"
xmlns:pim="http://prismstandard.org/namespaces/pim/2.2/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:prl="http://prismstandard.org/namespaces/prl/2.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:pur="http://prismstandard.org/namespaces/prismusagerights/2.1/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PARA">
<p>
<xsl:apply-templates select="@* | node()"/>
</p>
</xsl:template>
<xsl:template match="@PARA">
<xsl:attribute name="p">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PMISSN">
<prism:issn>
<xsl:apply-templates select="@* | node()"/>
</prism:issn>
</xsl:template>
<xsl:template match="@PMISSN">
<xsl:attribute name="prism:issn">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
以下是我希望转换的文件示例:
<CNVOUTPUT>
<ACTCD>A</ACTCD><TXTAVAIL>STORED</TXTAVAIL><TXTRIGHTS>YES</TXTRIGHTS><TPGAVAIL>NOT STORED</TPGAVAIL><TPGRIGHTS>NO</TPGRIGHTS><FPIAVAIL>STORED</FPIAVAIL><FPIRIGHTS>NO</FPIRIGHTS><PMINFO><PMID>17549</PMID><PMTTL><PTTL>Mag's</PTTL></PMTTL><PMQUAL>Toronto</PMQUAL><PMJCD>GMAC</PMJCD><PMISSN>00249262</PMISSN>
<PMCODEN>MCNMBC</PMCODEN><PMSTYP><PSCD>2</PSCD><PSDES>PERIODICAL</PSDES></PMSTYP></PMINFO><PCINFO><PCDATE><PCNDATE>19920127</PCNDATE><PCADATE>Jan 27, 1992</PCADATE></PCDATE><STDCITE><PCVOL>105</PCVOL><PCISSU>4</PCISSU></STDCITE></PCINFO><DOCINFO><DOCID>1703718</DOCID><CPR>Copyright Maclean Hunter Consumer Publications Jan 27, 1992</CPR><DTTL><TTL>Public Remarks on a Private Matter</TTL></DTTL><DCITE><PGC>1</PGC><SP>9</SP><DOCL>English</DOCL><DPG>9</DPG><XID>CBCAMCLE2678804</XID></DCITE><AUS><AU><AUNAME>Amiel, Barbara</AUNAME></AU></AUS><DOCTY>Commentary</DOCTY></DOCINFO><TEXT><TXT><PARA>No Text Available.</PARA></TXT></TEXT><IDX><ITY>General Subject Terms</ITY><ITM><DTY>General Subject Term</DTY><DTA>Journalism</DTA></ITM><ITM><DTY>General Subject Term</DTY><DTA>Journalistic ethics</DTA></ITM><ITY>Classification Codes</ITY><ITM><DTY>Classification Code</DTY><DTA>9172</DTA></ITM><ITM><DTY>Classification Expansion</DTY><DTA>Canada</DTA></ITM></IDX><ABS><SABS><PARA>A woman discusses her run-ins with with Canadian media. Making matters more complicated is the fact that she is also a journalist. Other journalists started writing about her past relationships. She attributes the controversy to her success in the field.</PARA></SABS></ABS>
</CNVOUTPUT>
答案 0 :(得分:0)
您的代码中只有一个错误。应将所有名称空间声明为根元素xsl:stylesheet
:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pam="http://prismstandard.org/namespaces/pam/2.2/"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:prism="http://prismstandard.org/namespaces/basic/2.2/"
xmlns:pim="http://prismstandard.org/namespaces/pim/2.2/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:prl="http://prismstandard.org/namespaces/prl/2.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:pur="http://prismstandard.org/namespaces/prismusagerights/2.1/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
exclude-result-prefixes="dc pur rdf prl dcterms pim prism xhtml pam">
您可以在此处看到它正常工作:http://xsltransform.net/3NzcBtc/1
要从代码中删除不需要的命名空间声明,应使用exclude-result-prefixes
。它列出了要排除的所有前缀。
P.S。有一个更短的XSLT 2.0替代exclude-result-prefixes="#all"
如果您只想在根元素中使用两个名称空间声明而不是每个具有名称空间的单独元素,那么这应该有效:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:prism="http://prismstandard.org/namespaces/basic/2.2/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
您可以在此处查看结果:http://xsltransform.net/3NzcBtc/4