【问题标题】:XSLT to rename qualified root element, keep other namespacesXSLT 重命名合格的根元素,保留其他名称空间
【发布时间】:2015-02-13 20:14:27
【问题描述】:

我正在尝试编写一个 XSLT 来操作我们需要的 XML:

  • 将合格的根元素从“tool:view”重命名为“indexes”
  • 将“tool:view”属性从“name”重命名为“view”
  • 将默认命名空间从“http://company/server/views/index”更改为“http://company/views/index
  • 删除工具命名空间保留其他命名空间(xsi、atom)

我很难让命名空间正常运行。我已经通过许多不同的帖子来解决这个问题,但没有一个是我的场景。

原文:

<?xml version="1.0" encoding="UTF-8"?>
<tool:view name="viewindexes" xmlns="http://company/server/views/index" xmlns:tool="http://company" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <index>
        <index>ABC</index>
        <description>ABC Description</description>
        <atom:link rel="self" href="http://server/admin/views/index/ABC?$format=xml"/>
    </index>
    <index>
        <index>DEF</index>
        <description>DEF Description</description>
        <atom:link rel="self" href="http://server/admin/views/index/DEF?$format=xml"/>
    </index>
    <atom:link rel="self" href="http://server/admin/views/index/DEF?$format=xml&amp;$count=2"/>
    <atom:link rel="next" title="Next interval" href="?$format=xml&amp;$count=2&amp;$start_index=2"/>
</tool:view>

预期:

<?xml version="1.0" encoding="UTF-8"?>
<indexes view="viewindexes" xmlns="http://company/views/index" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <index>
        <index>ABC</index>
        <description>ABC Description</description>
        <atom:link rel="self" href="http://server/admin/views/index/ABC?$format=xml"/>
    </index>
    <index>
        <index>DEF</index>
        <description>DEF Description</description>
        <atom:link rel="self" href="http://server/admin/views/index/DEF?$format=xml"/>
    </index>
    <atom:link rel="self" href="http://server/admin/views/index/DEF?$format=xml&amp;$count=2"/>
    <atom:link rel="next" title="Next interval" href="?$format=xml&amp;$count=2&amp;$start_index=2"/>
</indexes>

XSLT:

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:variable name="root" select="'indexes'" /> 
        <xsl:variable name="ns" select="'http://company/views/index'" /> 

        <xsl:template match="/*:view">
            <xsl:element name="{$root}" namespace="{$ns}">
                <xsl:attribute name="view">
                    <xsl:value-of select="@name"/>
                </xsl:attribute>
                <xsl:apply-templates />
            </xsl:element>
        </xsl:template>

        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" />
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>

结果:

<?xml version="1.0" encoding="UTF-8"?>
<indexes xmlns="http://company/views/index" view="viewindexes">
        <index xmlns="http://company/server/views/index" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:tool="http://company" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <index>ABC</index>
            <description>ABC Description</description>
            <atom:link href="http://server/admin/views/index/ABC?$format=xml" rel="self"/>
        </index>
        <index xmlns="http://company/server/views/index" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:tool="http://company" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <index>DEF</index>
            <description>DEF Description</description>
            <atom:link href="http://server/admin/views/index/DEF?$format=xml" rel="self"/>
        </index>
        <atom:link xmlns="http://company/server/views/index" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:tool="http://company" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" href="http://server/admin/views/index/DEF?$format=xml&amp;$count=2" rel="self"/>
        <atom:link xmlns="http://company/server/views/index" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:tool="http://company" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" href="?$format=xml&amp;$count=2&amp;$start_index=2" rel="next" title="Next interval"/>
    </indexes>

【问题讨论】:

    标签: xml xslt namespaces


    【解决方案1】:

    假设我建议使用 XSLT 2.0

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns="http://company/views/index"
      xmlns:tool="http://company"
      xmlns:svi="http://company/server/views/index"
      exclude-result-prefixes="tool svi"
    >
    
            <xsl:template match="@* | node()">
              <xsl:copy copy-namespaces="no">
                <xsl:apply-templates select="@* | node()"/>
              </xsl:copy>
            </xsl:template>
    
            <xsl:template match="/tool:view">
                <indexes view="{@name}">
                    <xsl:copy-of select="namespace::*[not(. = ('http://company', 'http://company/server/views/index'))]"/>
                    <xsl:apply-templates />
                </indexes>
            </xsl:template>
    
            <xsl:template match="svi:*">
              <xsl:element name="{local-name()}">
                    <xsl:apply-templates select="@*|node()" />
              </xsl:element>
            </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 2013-11-16
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 2011-02-10
      • 2010-10-27
      • 2012-08-16
      • 2012-03-28
      • 2021-11-10
      相关资源
      最近更新 更多