在 xslt v2.0 的内部父组中添加命名空间 [英] Adding namespace in inner parent group in xslt v2.0

查看:26
本文介绍了在 xslt v2.0 的内部父组中添加命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了我在互联网上看到的所有具有相关要求的代码.但是,就我而言,我还需要填充内部父组中的命名空间.我的 XSLT 没有按预期工作.谁能帮我这个?谢谢.

I tried all the codes I've seen in the internet with relevant requirement as I have. However, in my case, I also need to populate the namespace within the inner parent group. My XSLT didn't work as expected. Can anyone help me with this? Thank you.

XSLT 代码:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="Section">
    <Section xmlns="www.hdgd.co">
        <xsl:apply-templates select="@*|node()"/>
    </Section>
</xsl:template>

输入:

<Record xmlns="www.hdgd.co">
<Data>
    <Section>
        <ID>1234DFD57</ID>
    </Section>
</Data>

预期输出:

<Record>
<Data>
    <Section xmlns="www.hdgd.co">
        <ID>1234DFD57</ID>
    </Section>
</Data>

生成的输出:

<Record xmlns="www.hdgd.co">
<Data>
    <Section>
        <ID>1234DFD57</ID>
    </Section>
</Data>

推荐答案

您似乎不知道命名空间继承.Record 根元素中的默认命名空间声明应用于输入文档的所有 元素.因此,为了获得请求的结果,您必须将所有元素从其命名空间中取出,同时不处理 Section 元素及其后代:

You seem to be unaware of namespaces inheritance. The default namespace declaration at the Record root element is applied to all elements of the input document. Therefore, in order to achieve the requested result, you must take all elements out of their namespace, while leaving the Section element and its descendants unprocessed:

XSLT 2.0

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="www.hdgd.co"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="Section">
    <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet> 

<小时>

添加:

如果您的输入有需要复制的属性,则将第一个模板更改为:


Added:

If your input has attributes that need copying, then change the first template to:

<xsl:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

这篇关于在 xslt v2.0 的内部父组中添加命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆