如何控制xslt中的值

时间:2011-06-23 16:25:14

标签: xml xslt xpath

如何连接以下NVBusinessId数据

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

4 个答案:

答案 0 :(得分:0)

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>

输出:

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

答案 1 :(得分:0)

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

产生这个结果:

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

答案 2 :(得分:0)

在XSLT 2.0中,生活似乎更容易一些。您可以直接使用xsl:value-of(最终还指定一个分隔符):

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

答案 3 :(得分:0)

这个简短而完整的转换基于最基本的XSLT设计模式:覆盖身份规则

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

应用于提供的XML文档

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

产生了想要的正确结果

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