【问题标题】:xsl add new elements with namespacesxsl 添加带有命名空间的新元素
【发布时间】:2014-11-03 08:55:50
【问题描述】:

我有这个 xml:

<Row>

<one>1</one>
<two>2</two>
<tree>3</tree>
<four>4</four>
<five>5</five>

</Row>

我希望得到结果:

<n0:Result xmlns:ns0="http://www.my.schemas/schemas/event1.xsd"
           xmlns:ns1="http://www.my.schemas/schemas/event2.xsd"
           xmlns:ns2="http://www.my.schemas/schemas/event3.xsd">
    <n1:group1>
        <n2:one>1</n2:one>
        <n2:two>2</n2:two>
    </n1:group1>

    <n1:group2>
        <n2:tree>3</n2:tree>
        <n2:four>4</n2:four>
    </n1:group2>

    <n0:five>5</n0:five>

</Result>

我现在的 xsl 是:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
           xmlns:ns0="http://www.my.schemas/schemas/event1.xsd"
           xmlns:ns1="http://www.my.schemas/schemas/event2.xsd"
           xmlns:ns2="http://www.my.schemas/schemas/event3.xsd" >
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>


    <xsl:template match="Row">
    <result>
                <group1>
                    <xsl:apply-templates select="one|two"/>
                </group1>
                <group2>
                    <xsl:apply-templates select="tree|four"/>
                </group2>

                <xsl:apply-templates select="five"/>
    </result>   
    </xsl:template>

</xsl:stylesheet>

如何将所需的命名空间添加到新旧元素? 我不知道如何使用分组和新元素来做到这一点,只知道现有元素。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    只需按照您在所需示例中的字面意思编写这些元素即可

    <xsl:template match="Row">
    <result>
                <group1>
                    <xsl:apply-templates select="one|two"/>
                </group1>
                <group2>
                    <xsl:apply-templates select="tree|four"/>
                </group2>
    
                <xsl:apply-templates select="five"/>
    </result>   
    </xsl:template>
    

    变成

    <xsl:template match="Row">
    <ns0:result>
                <ns1:group1>
                    <xsl:apply-templates select="one|two"/>
                </ns1:group1>
                <ns1:group2>
                    <xsl:apply-templates select="tree|four"/>
                </ns1:group2>
    
                <xsl:apply-templates select="five"/>
    </ns0:result>   
    </xsl:template>
    

    显然,您需要确保其他模板也在您希望它们所属的命名空间中输出它们的元素。

    一个更完整的例子是

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
               xmlns:ns0="http://www.my.schemas/schemas/event1.xsd"
               xmlns:ns1="http://www.my.schemas/schemas/event2.xsd"
               xmlns:ns2="http://www.my.schemas/schemas/event3.xsd" >
        <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
    
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
    
    
    <xsl:template match="Row">
    <ns0:result>
                <ns1:group1>
                    <xsl:apply-templates select="one|two"/>
                </ns1:group1>
                <ns1:group2>
                    <xsl:apply-templates select="tree|four"/>
                </ns1:group2>
    
                <xsl:apply-templates select="five"/>
    </ns0:result>   
    </xsl:template>
    
    </xsl:stylesheet>
    

    那么,正如我所说,如果你改造其他元素,你会添加模板,所以你也需要

    <xsl:template match="one | two">
      <xsl:element name="ns1:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>
    

    <xsl:template match="three | four">
      <xsl:element name="ns2:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>
    

    <xsl:template match="five">
      <xsl:element name="ns0:{local-name()}">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>
    

    就是这样

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
               xmlns:ns0="http://www.my.schemas/schemas/event1.xsd"
               xmlns:ns1="http://www.my.schemas/schemas/event2.xsd"
               xmlns:ns2="http://www.my.schemas/schemas/event3.xsd" >
        <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
    
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
    
    
    <xsl:template match="Row">
    <ns0:result>
                <ns1:group1>
                    <xsl:apply-templates select="one|two"/>
                </ns1:group1>
                <ns1:group2>
                    <xsl:apply-templates select="tree|four"/>
                </ns1:group2>
    
                <xsl:apply-templates select="five"/>
    </ns0:result>   
    </xsl:template>
    
        <xsl:template match="one | two">
          <xsl:element name="ns1:{local-name()}">
            <xsl:apply-templates/>
          </xsl:element>
        </xsl:template>
    
    
    
        <xsl:template match="three | four">
          <xsl:element name="ns2:{local-name()}">
            <xsl:apply-templates/>
          </xsl:element>
        </xsl:template>
    
    
    
        <xsl:template match="five">
          <xsl:element name="ns0:{local-name()}">
            <xsl:apply-templates/>
          </xsl:element>
        </xsl:template>
    
    
    </xsl:stylesheet>
    

    【讨论】:

    • 我试过了,但如果你只是在 xsl 中写 n0:result 你会得到 NO n0 的结果
    • @Ishaked,我已经编辑了答案并试图展示如何转换其他元素,以便它们最终进入命名空间。
    猜你喜欢
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2019-11-17
    • 2017-11-27
    • 2012-11-25
    • 1970-01-01
    相关资源
    最近更新 更多