XSL - 无法将命名空间添加到根节点
我有一个 html,我想使用 html-agility-pack 库将其解析为 xml。这是 xsl:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<BusinessDetail>
<Name>
<xsl:value-of select="//span[@class='pp-place-title']/span" />
</Name>
<Address>
<xsl:value-of select="//span[@class='pp-headline-item pp-headline-address']/span"/>
</Address>
...
</BusinessDetail>
</xsl:template>
</xsl:stylesheet>
我只想向根节点添加一个命名空间。预期的输出是:
<BusinessDetail xmlns:g="http://myurl.com">
<Name>
...
</Name>
..
</BusinessDetail>
因此将我的 xls 更改为:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<BusinessDetail xmlns:g="http://myurl.com">
<Name>
<xsl:value-of select="//span[@class='pp-place-title']/span" />
</Name>
<Address>
<xsl:value-of select="//span[@class='pp-headline-item pp-headline-address']/span"/>
</Address>
...
</BusinessDetail>
</xsl:template>
</xsl:stylesheet>
但命名空间不会出现在输出中。有什么问题吗?
I have a html, I want to parse it to xml using html-agility-pack library. Here's the xsl:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<BusinessDetail>
<Name>
<xsl:value-of select="//span[@class='pp-place-title']/span" />
</Name>
<Address>
<xsl:value-of select="//span[@class='pp-headline-item pp-headline-address']/span"/>
</Address>
...
</BusinessDetail>
</xsl:template>
</xsl:stylesheet>
I just want to add a namespace to the root node. The expected output is:
<BusinessDetail xmlns:g="http://myurl.com">
<Name>
...
</Name>
..
</BusinessDetail>
So change my xls to:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<BusinessDetail xmlns:g="http://myurl.com">
<Name>
<xsl:value-of select="//span[@class='pp-place-title']/span" />
</Name>
<Address>
<xsl:value-of select="//span[@class='pp-headline-item pp-headline-address']/span"/>
</Address>
...
</BusinessDetail>
</xsl:template>
</xsl:stylesheet>
But the namespace does not appear in the output. Is there something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕您尝试创建的输出没有任何意义!考虑以下文档的含义:
您正在定义命名空间前缀“g”并将其映射到 URI
http://myurl.com
,但是,您的文档不包含任何以下元素:定义为在此名称空间内! XSLT 的输出完全正确地删除了这个冗余的名称空间声明。您是否打算这样做:这可确保
BusinessDetail
及其所有子元素都位于http://myurl.com
命名空间中。I'm afraid that the output you are trying to create makes no sense! Think about the meaning of the following document:
You are defining a namespace prefix 'g' and mapping it to the URI
http://myurl.com
, however, your document does not contain any elements that are defined as being within this namespace! The output of the XSLT is quite rightfully stripping out this redundant namespace declaration. Did you mean to do this intead:This ensures that
BusinessDetail
and all its child elements are in thehttp://myurl.com
namespace.您需要将命名空间添加到样式表中
,但是,除非您指定使用该命名空间的任何节点(您的示例没有指定,除了定义它之外),否则它将不会显示在输出中。
You need to add the namespace to the stylesheet
However, unless you specify any node that uses that namespace (your example doesn't, beyond defining it) it will not show in the output.