我是XSL的新手,想要将NewML G2格式的XML转换为另一种XML。如何处理XSLT中的命名空间?
比如我有:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
- Structure: NML2 SNI Text
-->
<!-- ========================================================= -->
<newsMessage xmlns="http://iptc.org/std/nar/2006-10-01/" xmlns:rtr="http://www.reuters.com/ns/2003/08/content" xmlns:x="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<transmitId>tag:123.com,0000:newsml_N19279043:609406403</transmitId>
<priority>3</priority>
<destination>ABX</destination>
</header>
<itemSet>
<newsItem conformance="power" guid="tag:reuters.com,0000:newsml_N19279043" standard="NewsML-G2" standardversion="2.1" version="609406403" xml:lang="en">
<itemMeta>
<itemClass qcode="icls:text" rtr:msgType="S"/>
<provider literal="reuters.com"/>
<versionCreated>2011-05-20T05:00:27.000Z</versionCreated>
</itemMeta>
<contentMeta>
<urgency>3</urgency>
<infoSource literal="Reuters" role="cRole:origProv"/>
<subject qcode="N2:BNK"/>
<headline>My Headline</headline>
<by>ABC</by>
</contentMeta>
<contentSet>
<inlineXML contenttype="application/xhtml+html" wordcount="881">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title/>
</head>
<body>
<p>Paragraph A</p>
<p>* Paragraph A</p>
</body>
</html>
</inlineXML>
</contentSet>
</newsItem>
</itemSet>
</newsMessage>
我想我的结果XML是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<MyData>
<MyTransmitId>tag:123.com,0000:newsml_N19279043:609406403</MyTransmitId>
<MyHeadline>My Headline</MyHeadline>
<MyContent>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title/>
</head>
<body>
<p>Paragraph A</p>
<p>* Paragraph A</p>
</body>
</html>
</MyContent>
</MyData>
我用下面的XSL出来:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" indent="yes" encoding="utf-8" />
<xsl:template match="/newsMessage">
<MyTransmitId>
<xsl:value-of select="header/transmitId"/>
</MyTransmitId>
<MyHeadline>
<xsl:value-of select="itemSet/newsItem/contentMeta/headline"/>
</MyHeadline>
<MyContent>
<xsl:value-of select="itemSet/newsItem/contentSet/inlineXML"/>
</MyContent>
</xsl:template>
</xsl:stylesheet>
然而它转化为不太正确的东西。而且我注意到它是因为该元素
<newsMessage xmlns="http://iptc.org/std/nar/2006-10-01/" xmlns:rtr="http://www.reuters.com/ns/2003/08/content" xmlns:x="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
的。如果我将其更改为以下,然后我的XSL将工作:
<newsMessage>
如何转变元素newsMessage用正确的命名空间?
非常感谢。
我编辑了标题,使其多了几分搜索友好。如果你喜欢旧的,请随时回滚编辑:) – 2011-06-01 07:54:47
@Merlyn:谢谢! – 2011-06-01 08:09:37