2011-10-08 36 views
0

我有这种结构的XML文件:复杂的根标签

<?xml version="1.0" encoding="utf-16"?> 
<CSCNASN xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AdvancedShipNotice.xsd"> 
    <RecordType0> 
    <Darta1 /> 
    <Darta2 /> 
    </RecordType0> 
    <RecordType1> 
    <Darta1 /> 
    <Darta2 /> 
    </RecordType1> 
</CSCNASN 

我想删除旁边的标签CSCNASN所有的部分(XMLNS:XSI =“http://www.w3.org/ 2001/XMLSchema-instance“xmlns:xsd =”http://www.w3.org/2001/XMLSchema“xmlns =”AdvancedShipNotice.xsd“),并创建一个具有相同结构的XML文件,但只需使用标记CSCNASN。

我尝试过这种转换,但它不工作,因为我想。我定义了一个变量来选择所有的xml标签来替换它。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:java="http://xml.apache.org/xslt/java" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="/message"> 
     <xsl:variable name="fileName"> 
      <xsl:value-of select="@number"/> 
     </xsl:variable> 
       <xsl:variable name="outermostElementName" select="name(/*)" /> 

       <xsl:variable name="prova">"CSCNASN\ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AdvancedShipNotice.xsd""</xsl:variable> 

      <message> 

      <xsl:for-each select="./@*"> 
       <xsl:copy /> 
      </xsl:for-each> 


      <xsl:for-each select="./$prova"> 
       <CSCNASN> 
        <xsl:attribute name="fileName"> 
            <xsl:value-of select="$fileName" /> 
           </xsl:attribute> 

        <xsl:apply-templates select="node()" /> 

          </CSCNASN> 

      </xsl:for-each> 
      </message> 
    </xsl:template> 

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

</xsl:stylesheet> 

回答

0

这将返回您的XML没有命名空间,猜猜这就是您需要的?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xx="AdvancedShipNotice.xsd"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:template match="/"> 
    <xsl:for-each select="xx:*"> 
     <xsl:call-template name="copy"/> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template name="copy"> 
    <xsl:element name="{local-name()}"> 
     <xsl:copy-of select="@*"/> 
     <xsl:value-of select="text()"/> 
     <xsl:for-each select="xx:*"> 
      <xsl:call-template name="copy"/> 
     </xsl:for-each> 
    </xsl:element> 
</xsl:template> 

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

返回以下内容:

<CSCNASN> 
<RecordType0> 
    <Darta1/> 
    <Darta2/> 
</RecordType0> 
<RecordType1> 
    <Darta1/> 
    <Darta2/> 
</RecordType1> 
</CSCNASN>