如何处理 XSLT 中的名称空间?
我是 XSL 新手,希望将 NewML G2 格式的 XML 转换为另一种 XML。
例如,我有:
<?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>
How do Itransform the element newsMessage with the namespaceproperty?
非常感谢。
I am new to XSL and would like to transform a NewML G2 format XML into another XML.
For example I have:
<?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>
I would like my result XML to be something like:
<?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>
I come out with the following 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>
However it transforms to something not quite right. And I noticed it is because of the element
<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">
If I change it to the below then my XSL will work:
<newsMessage>
How do I transform the element newsMessage with the namespaces properly?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有几个问题:
首先,您的大部分源文档都位于名为“http://iptc.org/std/nar/2006-10-01/”的命名空间中,在引用该文件时需要考虑到这一点XSLT 中的内容。在下面的样式表中,我通过将此命名空间绑定到前缀“itpc”,然后在 XPath 表达式中使用它来完成此操作。
其次,您希望将 XHTML 内容结构复制到结果中,并且需要使用。 (不是 value-of)来做到这一点 — 事实上,您需要获取 inlineXML 元素的 内容,而不是它本身;我已经相应地修改了 XPath。
A couple of problems here:
First, much of your source document is in the Namespace named "http://iptc.org/std/nar/2006-10-01/", and you need to take this into account when referencing that content in your XSLT. In the stylesheet below I have done that by binding this Namespace to the prefix "itpc", and then using that in the XPath expressions.
Secondly, you want the XHTML content structure to be copied into your result, and you need to use <xsl:copy-of> (not value-of) to do that — in fact you need to get the content of your inlineXML element, rather than it itself; I have modified the XPath accordingly.
声明命名空间并使用它。
Declare the namespace and use it.
我找到了另一种解决方案,为了其他人的利益而发布在这里:)
I found another solution to this one, posting here for the benefits of others :)