开发者

How to Concat the values in xslt

开发者 https://www.devze.com 2023-03-15 11:47 出处:网络
How to concat the below NVBusinessId data <FinalBusinessDetails> <BusinessIdDetails xmlns:ns0=\"http://www.portal.nv.gov/FinalCorporationDetails\"

How to concat the below NVBusinessId data

<FinalBusinessDetails>
<BusinessIdDetails xmlns:ns0="http://www.portal.nv.gov/FinalCorporationDetails" 
                   xmlns="http://www.portal.nv.gov/FinalCorporationDetails">
    <ns0:FinalBusinessIdDetails>
        <ns0:NVBusinessId>:NV0511201142</ns0:NVBusinessId>
    </ns0:FinalBusinessIdDetails>
    <ns0:FinalBusinessIdDetails>
        <ns0:NVBusinessId>:NV0511201143</ns0:NVBusinessId>
    </ns0:FinalBusinessIdDetails>
    <ns0:FinalBusinessIdDetails>
        <ns0:NVBusinessId>:NV20111227759</ns0:NVBusinessId>
    </ns0:FinalBusinessIdDetails>
    <ns0:FinalBusinessIdDetails>
        <ns0:NVBusinessId>:NV20111227763</ns0:NVBusinessId>
    </ns0:FinalBusinessIdDetails>
    <ns0:FinalBusinessIdDetails>
        <ns0:NVBusinessId>:NV20111227763</ns0:NVBusinessId>
 开发者_StackOverflow中文版   </ns0:FinalBusinessIdDetails>
    <ns0:FinalBusinessIdDetails>
        <ns0:NVBusinessId>:NV20111227759</ns0:NVBusinessId>
    </ns0:FinalBusinessIdDetails>
    <ns0:FinalBusinessIdDetails>
        <ns0:NVBusinessId>:</ns0:NVBusinessId>
    </ns0:FinalBusinessIdDetails>
    <ns0:FinalBusinessIdDetails>
        <ns0:NVBusinessId>:NV20111227802</ns0:NVBusinessId>
    </ns0:FinalBusinessIdDetails>
</BusinessIdDetails>

final out should be in

<FinalBusinessDetails>
   <BusinessIdDetails xmlns:ns0="http://www.portal.nv.gov/FinalCorporationDetails"
                      xmlns="http://www.portal.nv.gov/FinalCorporationDetails">
      <ns0:FinalBusinessIdDetails>
         <ns0:NVBusinessId>NV0511201142:NV0511201143:NV20111227759:NV20111227763</ns0:NVBusinessId>
      </ns0:FinalBusinessIdDetails>
   </BusinessIdDetails>
</FinalBusinessDetails>


XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns0="http://www.portal.nv.gov/FinalCorporationDetails"
    exclude-result-prefixes="ns0"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="FinalBusinessDetails">
    <FinalBusinessDetails>
      <BusinessIdDetails xmlns="http://www.portal.nv.gov/FinalCorporationDetails">
        <ns0:FinalBusinessIdDetails>
          <ns0:NVBusinessId>
            <xsl:apply-templates select="//ns0:FinalBusinessIdDetails"/>
          </ns0:NVBusinessId>
        </ns0:FinalBusinessIdDetails>
      </BusinessIdDetails>
    </FinalBusinessDetails>
  </xsl:template>


  <xsl:template match="ns0:FinalBusinessIdDetails">
    <xsl:value-of select="ns0:NVBusinessId/."/>
  </xsl:template>

</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="utf-8"?>
<FinalBusinessDetails>
  <BusinessIdDetails xmlns="http://www.portal.nv.gov/FinalCorporationDetails">
    <ns0:FinalBusinessIdDetails xmlns:ns0="http://www.portal.nv.gov/FinalCorporationDetails">
      <ns0:NVBusinessId>:NV0511201142:NV0511201143:NV20111227759:NV20111227763:NV20111227763:NV20111227759::NV20111227802</ns0:NVBusinessId>
    </ns0:FinalBusinessIdDetails>
  </BusinessIdDetails>
</FinalBusinessDetails>


This XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://www.portal.nv.gov/FinalCorporationDetails">

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

    <xsl:template match="text()">
        <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>

Produces this result:

<FinalBusinessDetails>
    <BusinessIdDetails xmlns="http://www.portal.nv.gov/FinalCorporationDetails" xmlns:ns0="http://www.portal.nv.gov/FinalCorporationDetails">
        <ns0:FinalBusinessIdDetails>:NV0511201142:NV0511201143:NV20111227759:NV20111227763:NV20111227763:NV20111227759::NV20111227802</ns0:FinalBusinessIdDetails>
    </BusinessIdDetails>
</FinalBusinessDetails>


In XSLT 2.0 life seems a bit easier. You can use the xsl:value-of directly (eventually specifying also a separator):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.portal.nv.gov/FinalCorporationDetails"
    xmlns:ns0="http://www.portal.nv.gov/FinalCorporationDetails"
    xpath-default-namespace="http://www.portal.nv.gov/FinalCorporationDetails"
    version="2.0">

    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="BusinessIdDetails">
        <xsl:copy>
            <ns0:FinalBusinessIdDetails>
                <ns0:NVBusinessId>
                    <xsl:value-of select="//NVBusinessId/text()"/>
                </ns0:NVBusinessId>
            </ns0:FinalBusinessIdDetails>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>


This short and complete transformation is based on the most basic XSLT design pattern: overriding the identity rule:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns0="http://www.portal.nv.gov/FinalCorporationDetails">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="*[1]/ns0:NVBusinessId">
  <xsl:copy>
   <xsl:apply-templates select="/*/*/*/ns0:NVBusinessId/text()"/>
  </xsl:copy>
 </xsl:template>
 <xsl:template match="ns0:FinalBusinessIdDetails[position() >1]"/>
</xsl:stylesheet>

when applied on the provided XML document:

<FinalBusinessDetails>
    <BusinessIdDetails xmlns:ns0="http://www.portal.nv.gov/FinalCorporationDetails" xmlns="http://www.portal.nv.gov/FinalCorporationDetails">
        <ns0:FinalBusinessIdDetails>
            <ns0:NVBusinessId>:NV0511201142</ns0:NVBusinessId>
        </ns0:FinalBusinessIdDetails>
        <ns0:FinalBusinessIdDetails>
            <ns0:NVBusinessId>:NV0511201143</ns0:NVBusinessId>
        </ns0:FinalBusinessIdDetails>
        <ns0:FinalBusinessIdDetails>
            <ns0:NVBusinessId>:NV20111227759</ns0:NVBusinessId>
        </ns0:FinalBusinessIdDetails>
        <ns0:FinalBusinessIdDetails>
            <ns0:NVBusinessId>:NV20111227763</ns0:NVBusinessId>
        </ns0:FinalBusinessIdDetails>
        <ns0:FinalBusinessIdDetails>
            <ns0:NVBusinessId>:NV20111227763</ns0:NVBusinessId>
        </ns0:FinalBusinessIdDetails>
        <ns0:FinalBusinessIdDetails>
            <ns0:NVBusinessId>:NV20111227759</ns0:NVBusinessId>
        </ns0:FinalBusinessIdDetails>
        <ns0:FinalBusinessIdDetails>
            <ns0:NVBusinessId>:</ns0:NVBusinessId>
        </ns0:FinalBusinessIdDetails>
        <ns0:FinalBusinessIdDetails>
            <ns0:NVBusinessId>:NV20111227802</ns0:NVBusinessId>
        </ns0:FinalBusinessIdDetails>
    </BusinessIdDetails>
</FinalBusinessDetails>

the wanted, correct result is produced:

<FinalBusinessDetails>
   <BusinessIdDetails xmlns="http://www.portal.nv.gov/FinalCorporationDetails" xmlns:ns0="http://www.portal.nv.gov/FinalCorporationDetails">
      <ns0:FinalBusinessIdDetails>
         <ns0:NVBusinessId>:NV0511201142:NV0511201143:NV20111227759:NV20111227763:NV20111227763:NV20111227759::NV20111227802</ns0:NVBusinessId>
      </ns0:FinalBusinessIdDetails>
   </BusinessIdDetails>
</FinalBusinessDetails>
0

精彩评论

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