XSL XML转换,改变命名空间值

时间:2015-03-10 15:48:50

标签: java xslt namespaces xslt-1.0 saxon

我正在尝试使用xsl从xml中删除一些元素,并尝试从sub1元素中声明的更改命名空间值。问题是,当我更改名称空间值时,旧的名称空间声明被插入到子元素(示例中的sub2)中,如何更改代码以防止它?

我做了这个例子,因为我无法显示真实的代码

的test.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet version="1.0" href="test.xsl"?>
<lamp:rootElement xmlns:lamp="adgfdgfhdsadfse">
<pref:sub1 ID="someId" xmlns:pref="http://www.myMountain.org/blabla">
    <pref:sub2>
    Today is tuesday
        <pref:sub3 att="someAttribute">
        Some text
            <pref:sub4>
                <pref:emptyElement/>
            </pref:sub4>
        </pref:sub3>
    </pref:sub2>
</pref:sub1>
</lamp:rootElement>

test.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:oldPref="http://www.myMountain.org/blabla"
xmlns:pref="http://www.myHill.org/blabla"
exclude-result-prefixes="oldPref">

<!-- For deleting element "sub4" -->
<xsl:template match="oldPref:sub1/oldPref:sub2/oldPref:sub3/oldPref:sub4"/>

<!-- Now i'm replacing the value of "pref" namespace (by creating a new element) -->
<xsl:template match="oldPref:sub1">

    <pref:sub1>
        <!-- Now i'm copying the elements from old "sub1" to the new one -->
        <xsl:apply-templates select="@*|node()"/>
    </pref:sub1>

</xsl:template>

<!-- Now im copying the rest of the xml file -->
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

错误的结果是我得到了result.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet version="1.0" href="test.xsl"?>
<lamp:rootElement xmlns:lamp="adgfdgfhdsadfse">
<pref:sub1 xmlns:pref="http://www.myHill.org/blabla" ID="someId">
    <pref:sub2 xmlns:pref="http://www.myMountain.org/blabla">
    Today is tuesday
        <pref:sub3 att="someAttribute">
        Some text

        </pref:sub3>
    </pref:sub2>
</pref:sub1>
</lamp:rootElement>

期望的结果

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet version="1.0" href="test.xsl"?>
<lamp:rootElement xmlns:lamp="adgfdgfhdsadfse">
<pref:sub1 xmlns:pref="http://www.myHill.org/blabla" ID="someId">
    <pref:sub2>
    Today is tuesday
        <pref:sub3 att="someAttribute">
        Some text

        </pref:sub3>
    </pref:sub2>
</pref:sub1>
</lamp:rootElement>

我的撒克逊人

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Project-Name: Saxon-HE
Created-By: 1.7.0_05-b06 (Oracle Corporation)
Main-Class: net.sf.saxon.Transform

这是我正在使用的命令

java net.sf.saxon.Transform -s:test.xml -xsl:test.xsl -o:result.xml

1 个答案:

答案 0 :(得分:1)

这里的问题是您要更改pref:sub1上的命名空间,而不是任何其他pref:元素上的命名空间。你需要更通用的东西:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:oldPref="http://www.myMountain.org/blabla"
                xmlns:pref="http://www.myHill.org/blabla"
                exclude-result-prefixes="oldPref">

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

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

  <xsl:template match="oldPref:sub4" />

</xsl:stylesheet>

在样本输入上运行时,结果为:

<?xml-stylesheet version="1.0" href="test.xsl"?>
<lamp:rootElement xmlns:lamp="adgfdgfhdsadfse">
  <pref:sub1 ID="someId" xmlns:pref="http://www.myHill.org/blabla">
    <pref:sub2>
      Today is tuesday
      <pref:sub3 att="someAttribute">
        Some text

      </pref:sub3>
    </pref:sub2>
  </pref:sub1>
</lamp:rootElement>