0

你能和我分享一个实现以下功能的 XSLT 吗?

输入:

<alfa data="abc" xmlns="http://test1.com/">
  <mus:beta xmlns:mus="http://test2.com">
    <mus:a>1234567897</mus:a>
    <mus:s>777666</mus:s>
  </mus:beta>
</alfa>

输出应该是:

<alfa data="abc" xmlns="http://test1.com/">
  <beta xmlns="http://test2.com">
    <a>1234567897</a>
    <s>777666</s>
  </beta>
</alfa>

实际上; 输入是用 XmlBeans 生成的;我无法用 xmlbeans 实现输出;所以我将在调解中使用 xslt 进行转换;但是我首先需要一个 xslt :) XmlBeans 解决方案也是可以接受的。:)

对于 xmlbeans 用户;以下不起作用,仅供参考:

Map map = new HashMap();
 map.put("http://test1.com/",""); 
 map.put("http://test2.com/","");
 xo.setSaveSuggestedPrefixes(map);

干杯,卡恩

4

2 回答 2

1

这是一个样式表:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:mus="http://test2.com"
  exclude-result-prefixes="mus"
  version="1.0">

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

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

</xsl:stylesheet>
于 2012-06-08T10:22:27.607 回答
1

如果您向文档显式添加名称空间声明,XmlBeans 将尊重它。您可以使用XmlCursor API 添加新的默认命名空间中间文档。例如,

    XmlObject xobj = XmlObject.Factory.parse(
            "<a xmlns='testA'>\n" +
            "  <B:b xmlns:B='testB'>\n" +
            "    <B:x>12345</B:x>\n" +
            "  </B:b>\n" +
            "</a>");

    // Use xpath with namespace declaration to find <B:b> element.
    XmlObject bobj = xobj.selectPath(
            "declare namespace B='testB'" +
            ".//B:b")[0];

    XmlCursor cur = null;
    try
    {
        cur = bobj.newCursor();
        cur.removeAttribute(new QName("http://www.w3.org/2000/xmlns/", "B"));
        cur.toNextToken();
        cur.insertNamespace("", "testB");
    }
    finally
    {
        cur.dispose();
    }

    System.out.println(xobj.xmlText());
于 2012-06-08T16:09:59.487 回答