如何处理 XSLT 中的名称空间?

发布于 2024-11-10 12:34:31 字数 3736 浏览 0 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(3

我要还你自由 2024-11-17 12:34:32

这里有几个问题:

首先,您的大部分源文档都位于名为“http://iptc.org/std/nar/2006-10-01/”的命名空间中,在引用该文件时需要考虑到这一点XSLT 中的内容。在下面的样式表中,我通过将此命名空间绑定到前缀“itpc”,然后在 XPath 表达式中使用它来完成此操作。

其次,您希望将 XHTML 内容结构复制到结果中,并且需要使用。 (不是 value-of)来做到这一点 — 事实上,您需要获取 inlineXML 元素的 内容,而不是它本身;我已经相应地修改了 XPath。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:itpc="http://iptc.org/std/nar/2006-10-01/">
    <xsl:output method="xml" indent="yes" encoding="utf-8"/>
    <xsl:template match="/itpc:newsMessage">
        <MyTransmitId>
            <xsl:value-of
                select="itpc:header/itpc:transmitId"/>
        </MyTransmitId>
        <MyHeadline>
            <xsl:value-of
                select="itpc:itemSet/itpc:newsItem/itpc:contentMeta/itpc:headline"/>
        </MyHeadline>
        <MyContent>
            <xsl:copy-of
                select="itpc:itemSet/itpc:newsItem/itpc:contentSet/itpc:inlineXML/*"/>
        </MyContent>
    </xsl:template>
</xsl:stylesheet>

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.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:itpc="http://iptc.org/std/nar/2006-10-01/">
    <xsl:output method="xml" indent="yes" encoding="utf-8"/>
    <xsl:template match="/itpc:newsMessage">
        <MyTransmitId>
            <xsl:value-of
                select="itpc:header/itpc:transmitId"/>
        </MyTransmitId>
        <MyHeadline>
            <xsl:value-of
                select="itpc:itemSet/itpc:newsItem/itpc:contentMeta/itpc:headline"/>
        </MyHeadline>
        <MyContent>
            <xsl:copy-of
                select="itpc:itemSet/itpc:newsItem/itpc:contentSet/itpc:inlineXML/*"/>
        </MyContent>
    </xsl:template>
</xsl:stylesheet>
痴骨ら 2024-11-17 12:34:32

声明命名空间并使用它。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:nar="http://iptc.org/std/nar/2006-10-01/">
 ...
    <xsl:template match="/nar:newsMessage">
     ...

Declare the namespace and use it.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:nar="http://iptc.org/std/nar/2006-10-01/">
 ...
    <xsl:template match="/nar:newsMessage">
     ...
仄言 2024-11-17 12:34:32

我找到了另一种解决方案,为了其他人的利益而发布在这里:)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" media-type="text/html"/>
    <xsl:template match="/">
        <xsl:element name="MyData">
            <xsl:element name="MyTransmitId">
                <xsl:value-of select="//*[name()='transmitId']"/>
            </xsl:element>
            <xsl:element name="MyHeadline">
                <xsl:value-of select="//*[name()='headline']"/>
            </xsl:element>
            <xsl:element name="MyContent">
                <xsl:copy-of select="//*[name()='inlineXML']/*"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

I found another solution to this one, posting here for the benefits of others :)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" media-type="text/html"/>
    <xsl:template match="/">
        <xsl:element name="MyData">
            <xsl:element name="MyTransmitId">
                <xsl:value-of select="//*[name()='transmitId']"/>
            </xsl:element>
            <xsl:element name="MyHeadline">
                <xsl:value-of select="//*[name()='headline']"/>
            </xsl:element>
            <xsl:element name="MyContent">
                <xsl:copy-of select="//*[name()='inlineXML']/*"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文