0

我需要在xml下面进行转换(没有命名空间)

XML:

<?xml version="1.0" encoding="UTF-8"?>
<policy>
 <num-drivers>123</num-drivers>
 <risk-policy-ind>false</policy-ind>
 <premium-amt>23.00</premium-amt>
</policy>

风格:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" 
xmlns="http://aaa.bbb.com" xmlns:ptws="http://aaa.bbb.com" exclude-result-prefixes="xs fn xsl">
<xsl:namespace-alias stylesheet-prefix="ptws" result-prefix="#default"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" xpath-default-namespace="http://aaa.bbb.com"/>
<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="ptws:policy">
<auto-policy-dto>
    <xsl:copy-of select="ptws:num-drivers"/>
    <xsl:copy-of select="ptws:risk-policy-ind"/>
    <xsl:copy-of select="ptws:premium-amt"/>
</policy>
</xsl:template>

JAVA代码:

TransformerFactory tfactory = TransformerFactory.newInstance();
Source xslt = new StreamSource(IN_XSLT_FILE_NAME);
Transformer transformer = tfactory.newTransformer(xslt);

SAXSource source = new SAXSource(
                XMLReaderFactory.createXMLReader(),
                new InputSource(IN_XML_FILE_NAME));
transformer.transform(source, new StreamResult(OUT_XML_FILE_NAME)); 

输出 xml:

<?xml version="1.0" encoding="UTF-8"?>
    123
    false
    23.00

output.xml 中缺少元素。如何在转换时提供命名空间“ http://aaa.bbb.com ”?

预期输出.xml:

<?xml version="1.0" encoding="UTF-8"?><ptws:policy xmlns:ptws="http://aaa.bbb.com">
  <num-drivers xmlns="http://aaa.bbb.com">123</num-drivers>
  <risk-policy-ind xmlns="http://aaa.bbb.com">false</risk-policy-ind>
  <premium-amt xmlns="http://aaa.bbb.com">23.00</premium-amt>
</ptws:policy>

我试图做这样的事情:

public class XMLUtilsTest {

   private static final String IN_XML_FILE_NAME = "C:/sampleXML.xml";
   private static final String IN_XSLT_FILE_NAME = "C:/sample.xslt";

   private static final String OUT_XML_FILE_NAME = "C:/rc/output.xml";

   public static Boolean transformXML() throws SAXException, IOException, URISyntaxException, TransformerException {

    try {
        TransformerFactory tfactory = TransformerFactory.newInstance();
        Source xslt = new StreamSource(IN_XSLT_FILE_NAME);
        Transformer transformer = tfactory.newTransformer(xslt);

        SAXSource source = new SAXSource(
                new NamespaceFilter(XMLReaderFactory.createXMLReader()),
                new InputSource(IN_XML_FILE_NAME));
        transformer.transform(source, new StreamResult(OUT_XML_FILE_NAME));            
    }
    catch(Exception ex){
        System.out.println(ex.getMessage()); 
    }                    

  }

protected static class NamespaceFilter extends XMLFilterImpl {

    String requiredNamespace = "http://aaa.bbb.com";

    public NamespaceFilter(XMLReader parent) {
        super(parent);
    }

    @Override
    public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
        if(arg0 != requiredNamespace) arg0 = requiredNamespace;
        super.startElement(arg0, arg1, arg2, arg3);
    }       
}
}

但这会产生以下 xml。仍然缺少元素

<?xml version="1.0" encoding="UTF-8"?><ptws:policy xmlns:ptws="http://aaa.bbb.com">
< xmlns="http://aaa.bbb.com">123</>
< xmlns="http://aaa.bbb.com">false</>
< xmlns="http://aaa.bbb.com">23.00</>
</ptws:policy>
4

1 回答 1

0

我认为您只是想更改所有元素的名称空间?

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

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

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

</xsl:stylesheet>

您的模板不起作用的原因是因为<xsl:template match="ptws:policy">不匹配任何内容,并且默认模板只是复制文本节点。

于 2013-02-18T23:40:15.530 回答