XSLT映射以删除命名空间

时间:2017-07-21 12:09:38

标签: xml xslt xsd

  1. 我是XSLT映射的新手并且卡住了。我了解一些功能的使用。 请建议阅读一些教程。
  2. 我的输入代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <ns0:hr xmlns:ns0="Namesapace.com">
    <Resources>
        <EmployeesRecords>
                <Record RecordId="1">
                        <Name>AAA € </Name>
                        <ID>111</ID>
                        <Phone>1111111111</Phone>
                </Record>
                <Record RecordId="2">
                        <Name>BBB</Name>
                        <ID>222</ID>
                        <Phone>2222222222</Phone>
                </Record>
        </EmployeesRecords>
        <ContractorsRecords>
                <Record RecordId="3">
                        <Name>ZZZ</Name>
                        <ID>999</ID>
                        <Phone>9999999999</Phone>
                </Record>
        </ContractorsRecords>
    </Resources>
    </ns0:hr>
    
  3. 我期待下面的内容,

    <?xml version="1.0" encoding="utf-8"?>
    <Resources>
    <EmployeesRecords>
        <Record RecordId="1">
            <Name>AAA € </Name>
            <ID>111</ID>
            <Phone>1111111111</Phone>
        </Record>
        <Record RecordId="2">
            <Name>BBB</Name>
            <ID>222</ID>
            <Phone>2222222222</Phone>
        </Record>
    </EmployeesRecords>
    <ContractorsRecords>
        <Record RecordId="3">
            <Name>ZZZ</Name>
            <ID>999</ID>
            <Phone>9999999999</Phone>
        </Record>
    </ContractorsRecords>
    </Resources>
    

    请建议。

2 个答案:

答案 0 :(得分:0)

从输入和输出中看到的实际转换规则是什么并不清楚。例如,您如何确定第一条记录应该

<Name>AAA € </Name>
<ID>111</ID>

而不是

<Name>AAA</Name>
<ID>€ 111</ID>

一旦确定了规则,使用xsl:analyze-string指令在XSLT 2.0中实现它们应该很简单。在XSLT 1.0中(许多人在18岁时仍然使用它)可能会更加困难。

至于你的教程请求,我只推荐我的书:来自Wiley / Wrox的XPath 2.0和XSLT 2.0程序员参考,第4版。

答案 1 :(得分:0)

从你的问题中不清楚你正在尝试做什么,但鉴于问题标题,以下XSLT应删除所有命名空间....

但请注意,命名空间是XML元素/属性名称的一部分,因此从中删除它们会从根本上改变XML文档。

<?xml version="1.0"?>
<!-- Created with Liquid Studio 2017 (https://www.liquid-technologies.com) -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>

    <!-- Stylesheet to remove all namespaces from a document -->
    <!-- NOTE: this will lead to attribute name clash, if an element contains
        two attributes with same local name but different namespace prefix -->
    <!-- Nodes that cannot have a namespace are copied as such -->

    <!-- template to copy elements -->
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>

    <!-- template to copy attributes -->
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

    <!-- template to copy the rest of the nodes -->
    <xsl:template match="comment() | text() | processing-instruction()">
        <xsl:copy/>
    </xsl:template>
</xsl:stylesheet>