Can you share an XSLT with me which achieves following please?

Input:

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

Output should be:

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

In fact; the input is generated with XmlBeans; I can not achieve the output with xmlbeans; So i will do a transform with xslt in mediation; however I need a xslt first :) XmlBeans solution is acceptable too. :)

For xmlbeans users; following does not work, fyi:

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

Cheers, Kaan

有帮助吗?

解决方案

Here is a stylesheet:

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

其他提示

If you explicitly add a namespace declaration to the document, XmlBeans will respect it. You can add a new default namespace mid-document by using the XmlCursor APIs. For example,

    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());
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top