在xslt v2.0中的内部父组中添加命名空间

时间:2017-05-29 06:04:17

标签: xslt xslt-2.0

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

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>

INPUT:

<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>

2 个答案:

答案 0 :(得分:1)

您似乎不知道名称空间继承。 Record根元素的默认命名空间声明应用于输入文档的所有元素。因此,为了实现请求的结果,必须将所有元素从其命名空间中删除,同时保留Section元素及其后代未处理:

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> 

加了:

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

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

答案 1 :(得分:0)

听起来好像要从RecordData删除命名空间:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
  xpath-default-namespace="www.hdgd.co">

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

    <xsl:template match="Record | Data">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>
</xsl:transform>

http://xsltransform.net/bEzjRJM给出了

<?xml version="1.0" encoding="UTF-8"?><Record>
<Data>
    <Section xmlns="www.hdgd.co">
        <ID>1234DFD57</ID>
    </Section>
</Data>
</Record>