我可以消除此转换输出中的命名空间声明吗?

发布于 2024-11-04 06:08:21 字数 1716 浏览 0 评论 0 原文

我想使用 XMLT 动态生成一个 XML 文件,该文件接受另一个 XML 文件作为输入。

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" />

 <xsl:template match="data">
   <xsl:apply-templates select="books" />
 </xsl:template>

 <xsl:template match="books">
   <books>
     <xsl:apply-templates select="chapters" />
   </books>
 </xsl:template>

<xsl:template match="chapters">
  <chapter name="{@name}" source="{@source}"
  xmlns:xi="http://www.w3c.org/2001/XInclude">
      <xsl:apply-templates select="pages" />
  </chapter>
</xsl:template>

 <xsl:template match="pages" xmlns:xi="http://www.w3.org/2001/XInclude">
   <xi:include>
      <xsl:value-of select="@name" />
    </xi:include>
 </xsl:template>

</xsl:stylesheet>

我想要的输出如下。

<chapter 
xmlns:xi="http://www.w3c.org/2001/XInclude" name="chapter1">
    <xi:include>page1</xi:include>
    <xi:include>page2</xi:include>
    <xi:include>page3</xi:include>
</chapter>

不幸的是,我的代码生成以下输出。

<chapter 
xmlns:xi="http://www.w3c.org/2001/XInclude" name="chapter1">
    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page1</xi:include>
    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page2</xi:include>
    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page3</xi:include>
</chapter>

有没有办法可以消除命名空间的实际 URI,因为我可以拥有 xi:include 标签而无需它们?

I would like to dynamically generate an XML file using XMLT that takes another XML file as input.

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" />

 <xsl:template match="data">
   <xsl:apply-templates select="books" />
 </xsl:template>

 <xsl:template match="books">
   <books>
     <xsl:apply-templates select="chapters" />
   </books>
 </xsl:template>

<xsl:template match="chapters">
  <chapter name="{@name}" source="{@source}"
  xmlns:xi="http://www.w3c.org/2001/XInclude">
      <xsl:apply-templates select="pages" />
  </chapter>
</xsl:template>

 <xsl:template match="pages" xmlns:xi="http://www.w3.org/2001/XInclude">
   <xi:include>
      <xsl:value-of select="@name" />
    </xi:include>
 </xsl:template>

</xsl:stylesheet>

My desired output is below.

<chapter 
xmlns:xi="http://www.w3c.org/2001/XInclude" name="chapter1">
    <xi:include>page1</xi:include>
    <xi:include>page2</xi:include>
    <xi:include>page3</xi:include>
</chapter>

Unfortunately, my code generates the output below.

<chapter 
xmlns:xi="http://www.w3c.org/2001/XInclude" name="chapter1">
    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page1</xi:include>
    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page2</xi:include>
    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page3</xi:include>
</chapter>

Is there a way I could eliminate the actual URI for the namespace for I can have xi:include tages without them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

旧情勿念 2024-11-11 06:08:21

xmlns:xi="http://www.w3.org/2001/XInclude" 移动到 xsl:stylesheet 元素:

<xsl:stylesheet version="1.0"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" />

 <xsl:template match="data">
   <xsl:apply-templates select="books" />
 </xsl:template>

 <xsl:template match="books">
   <books>
     <xsl:apply-templates select="chapters" />
   </books>
 </xsl:template>

<xsl:template match="chapters">
  <chapter name="{@name}" source="{@source}">
      <xsl:apply-templates select="pages" />
  </chapter>
</xsl:template>

 <xsl:template match="pages">
   <xi:include>
      <xsl:value-of select="@name" />
    </xi:include>
 </xsl:template>

</xsl:stylesheet>

Move the xmlns:xi="http://www.w3.org/2001/XInclude" to the xsl:stylesheet element:

<xsl:stylesheet version="1.0"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" />

 <xsl:template match="data">
   <xsl:apply-templates select="books" />
 </xsl:template>

 <xsl:template match="books">
   <books>
     <xsl:apply-templates select="chapters" />
   </books>
 </xsl:template>

<xsl:template match="chapters">
  <chapter name="{@name}" source="{@source}">
      <xsl:apply-templates select="pages" />
  </chapter>
</xsl:template>

 <xsl:template match="pages">
   <xi:include>
      <xsl:value-of select="@name" />
    </xi:include>
 </xsl:template>

</xsl:stylesheet>
生生漫 2024-11-11 06:08:21

请注意,您正在使用两个不同的命名空间 URI

http://www.w3.org/2001/XInclude

"

http://www.w3c.org/2001/XInclude

如果它们相同,则 命名空间修复过程 会解决这个问题。

对于此输入:

<books>
    <chapters name="chapter1">
        <pages name="page1"/>
        <pages name="page2"/>
        <pages name="page3"/>
    </chapters>
</books>

此样式表(您的样式表经过修改后的拼写错误):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="data">
        <xsl:apply-templates select="books" />
    </xsl:template>
    <xsl:template match="books">
        <books>
            <xsl:apply-templates select="chapters" />
        </books>
    </xsl:template>
    <xsl:template match="chapters">
        <chapter name="{@name}" source="{@source}"
         xmlns:xi="http://www.w3c.org/2001/XInclude">
            <xsl:apply-templates select="pages" />
        </chapter>
    </xsl:template>
    <xsl:template match="pages" xmlns:xi="http://www.w3c.org/2001/XInclude">
        <xi:include>
            <xsl:value-of select="@name" />
        </xi:include>
    </xsl:template>
</xsl:stylesheet>

输出:

<books>
    <chapter name="chapter1" source="" 
     xmlns:xi="http://www.w3c.org/2001/XInclude">
        <xi:include>page1</xi:include>
        <xi:include>page2</xi:include>
        <xi:include>page3</xi:include>
    </chapter>
</books>

Do note that your are using two different namespace URIs:

http://www.w3.org/2001/XInclude

and

http://www.w3c.org/2001/XInclude

If they were the same, the namespace fixup process would take care of that.

For this input:

<books>
    <chapters name="chapter1">
        <pages name="page1"/>
        <pages name="page2"/>
        <pages name="page3"/>
    </chapters>
</books>

This stylesheet (yours with amended typo):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="data">
        <xsl:apply-templates select="books" />
    </xsl:template>
    <xsl:template match="books">
        <books>
            <xsl:apply-templates select="chapters" />
        </books>
    </xsl:template>
    <xsl:template match="chapters">
        <chapter name="{@name}" source="{@source}"
         xmlns:xi="http://www.w3c.org/2001/XInclude">
            <xsl:apply-templates select="pages" />
        </chapter>
    </xsl:template>
    <xsl:template match="pages" xmlns:xi="http://www.w3c.org/2001/XInclude">
        <xi:include>
            <xsl:value-of select="@name" />
        </xi:include>
    </xsl:template>
</xsl:stylesheet>

Output:

<books>
    <chapter name="chapter1" source="" 
     xmlns:xi="http://www.w3c.org/2001/XInclude">
        <xi:include>page1</xi:include>
        <xi:include>page2</xi:include>
        <xi:include>page3</xi:include>
    </chapter>
</books>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文