XSL - 无法将命名空间添加到根节点

发布于 2024-11-08 05:36:55 字数 1444 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒夜笙歌凉 2024-11-15 05:36:55

恐怕您尝试创建的输出没有任何意义!考虑以下文档的含义:

<BusinessDetail xmlns:g="http://myurl.com">
  <Name>
    ...
  </Name>
  ..
</BusinessDetail>

您正在定义命名空间前缀“g”并将其映射到 URI http://myurl.com,但是,您的文档不包含任何以下元素:定义为在此名称空间内! XSLT 的输出完全正确地删除了这个冗余的名称空间声明。您是否打算这样做:

<BusinessDetail xmlns="http://myurl.com">
  <Name>
    ...
  </Name>
  ..
</BusinessDetail>

这可确保 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:

<BusinessDetail xmlns:g="http://myurl.com">
  <Name>
    ...
  </Name>
  ..
</BusinessDetail>

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:

<BusinessDetail xmlns="http://myurl.com">
  <Name>
    ...
  </Name>
  ..
</BusinessDetail>

This ensures that BusinessDetail and all its child elements are in the http://myurl.com namespace.

情深如许 2024-11-15 05:36:55

您需要将命名空间添加到样式表中

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:g="http://myurl.com">
  <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>

,但是,除非您指定使用该命名空间的任何节点(您的示例没有指定,除了定义它之外),否则它将不会显示在输出中。

You need to add the namespace to the stylesheet

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:g="http://myurl.com">
  <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>

However, unless you specify any node that uses that namespace (your example doesn't, beyond defining it) it will not show in the output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文