开发者

Complex Root Tag

开发者 https://www.devze.com 2023-04-11 21:20 出处:网络
I have an xml file with this structure: <?xml version=\"1.0\" encoding=\"utf-16\"?> <CSCNASN xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSch

I have an xml file with this structure:

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

I want to remove all the part next to the tag CSCNASN (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AdvancedShipNotice.xsd") and to create an xml file with the same structure but just with the tag CSCNASN.

I tried with this transformation but it is not working as I want. I defined a variable to select all the xml tag in order to replace it.

<?xml vers开发者_运维技巧ion="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>


This returns your XML without namespaces, guess that's what you need ?

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

it returns following :

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号