简体   繁体   English

如何使用 xslt 将父节点命名空间复制到子元素?

[英]How to copy parent node namespace to child element using xslt?

My xml looks like which I created using Java JAXBContext and Marshaller.我的 xml 看起来像我使用 Java JAXBContext 和 Marshaller 创建的。 I want to format some part of xml only not the whole xml.我想格式化 xml 的某些部分,而不是整个 xml。

<?xml version="1.0" encoding="UTF-8"?>
<ns4:Requests xmlns:ns2="http://www.dummy.com/xsd/tublu/murmur_001" xmlns:ns3="http://www.dummy.com/xsd/CommonObjects_001" xmlns:ns4="http://www.dummy.com/xsd/naku_001">
   <ns4:RequestSetId>fhskgvseruigiu</ns4:RequestSetId>
   <ns4:RequestStream>CHAPP</ns4:RequestStream>
   <ns4:Request>
      <ns4:TrackAndTrace>
         <ns4:CPAId>003</ns4:CPAId>
         <ns4:CorrelationId>ytuty</ns4:CorrelationId>
      </ns4:TrackAndTrace>
   </ns4:Request>
   <ns4:Request>
      <ns4:TrackAndTrace>
         <ns4:CPAId>003</ns4:CPAId>
         <ns4:CorrelationId>cyuri7</ns4:CorrelationId>
      </ns4:TrackAndTrace>
   </ns4:Request>
</ns4:Requests>

I want to format like我想格式化

<?xml version="1.0" encoding="UTF-8"?>
<ns4:Requests xmlns:ns2="http://www.dummy.com/xsd/tublu/murmur_001" xmlns:ns4="http://www.dummy.com/xsd/naku_001" xmlns:ns3="http://www.dummy.com/xsd/CommonObjects_001">
    <ns4:RequestSetId>fhskgvseruigiu</ns4:RequestSetId>
    <ns4:RequestStream>CHAPP</ns4:RequestStream>
    <ns4:Request xmlns:ns4="http://www.dummy.com/xsd/naku_001"><ns4:TrackAndTrace><ns4:CPAId>003</ns4:CPAId><ns4:CorrelationId>ytuty</ns4:CorrelationId></ns4:TrackAndTrace></ns4:Request>
    <ns4:Request xmlns:ns4="http://www.dummy.com/xsd/naku_001"><ns4:TrackAndTrace><ns4:CPAId>003</ns4:CPAId><ns4:CorrelationId>cyuri7</ns4:CorrelationId></ns4:TrackAndTrace></ns4:Request>
</ns4:Requests>

Here is the solution (by Transforming the XML Data using Java's XSLT APIs),这是解决方案(通过使用 Java 的 XSLT API 转换 XML 数据),

As you may also have noticed.. JAXB alone cannot meet this requirement, but after marshalling the object to a formatted XML String (as u have shown) you can then post process/transform it accordingly using a suitable XSLT file As you may also have noticed.. JAXB alone cannot meet this requirement, but after marshalling the object to a formatted XML String (as u have shown) you can then post process/transform it accordingly using a suitable XSLT file

So to get a linearized 'Request' element, just make use of the xsl shown below:因此,要获得线性化的“请求”元素,只需使用如下所示的 xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:strip-space elements="TrackAndTrace"/> 
    <xsl:strip-space elements="Request"/> 

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

</xsl:stylesheet>

Note: Also tested that above method/approach is working properly - used the Stylizer sample code (from https://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html )注意:还测试了上述方法/方法是否正常工作 - 使用了 Stylizer 示例代码(来自https://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.ZFC35EZ8888

Cheers!干杯!

Update: If you want a solution that also preserves the original namespace prefix as shown in your question, follow this variation Add factory.setNamespaceAware(true);更新:如果您想要一个还保留原始名称空间前缀的解决方案,如您的问题所示,请遵循此变体 Add factory.setNamespaceAware(true); in the Stylizer code & Use this tweaked XSLT在 Stylizer 代码中 & 使用这个经过调整的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform" xmlns:ns4="dummy.com/xsd/naku_001">
<xsl:strip-space elements="ns4:TrackAndTrace"/>
<xsl:strip-space elements="ns4:Request"/> 
   <xsl:template match="@*|node()">
       <xsl:copy>
           <xsl:apply-templates select="@*|node()"/>
       </xsl:copy>
   </xsl:template>
</xsl:stylesheet> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM