xml xslt help - 转换未正确呈现

时间:2013-02-08 06:01:58

标签: asp.net xml wcf xslt

我有一个像绑定一样的xml文件:

<?xml version="1.0" encoding="utf-16"?>
<MyCustomer>
  <Customers>
    <CutomerCode>C001</CutomerCode>
    <CustomerName>Shahbaz</CustomerName>
    <City>Karachi</City>
    <Country>Pakistan</Country>
  </Customers>
  <Customers>
    <CutomerCode>C002</CutomerCode>
    <CustomerName>Imran</CustomerName>
    <City>Lahore</City>
    <Country>Pakistan</Country>
  </Customers>
</MyCustomer>

我正在申请XSLT:

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

 <xsl:strip-space elements="*"/>
 <xsl:output indent="yes"/>
 <xsl:template match="MyCustomer">
   <xsl:copy>
     <Customers>
       <xsl:apply-templates select="Customers/*"/>
     </Customers>
   </xsl:copy>
 </xsl:template>
 <xsl:template match="Customers/*">
   <xsl:copy>
       <xsl:value-of select="."/>
   </xsl:copy>
 </xsl:template>
 <xsl:template match="Customers/City">
      <xsl:element name="Address">
         <xsl:element name="City">
       <xsl:value-of select="." />
        </xsl:element>
    </xsl:element>
 </xsl:template>
 <xsl:template match="Customers/Country">
      <xsl:element name="Address">
         <xsl:element name="Country">
       <xsl:value-of select="." />
        </xsl:element>
    </xsl:element>
 </xsl:template>
</xsl:stylesheet>

并且这个XSLT给了我结果:

<?xml version="1.0" encoding="utf-16"?>
<MyCustomer>
  <Customers>
    <CutomerCode>C001</CutomerCode>
    <CustomerName>Shahbaz</CustomerName>
    <Address>
      <City>Karachi</City>
    </Address>
    <Address>
      <Couontry>Pakistan</Couontry>
    </Address>
    <CutomerCode>C002</CutomerCode>
    <CustomerName>Imran</CustomerName>
    <Address>
      <City>Lahore</City>
    </Address>
    <Address>
      <Country>Pakistan</Country>
    </Address>
  </Customers>
</MyCustomer>

虽然我需要这样的转换:

<?xml version="1.0" encoding="utf-16"?>
<MyCustomer>
  <Customers>
    <CutomerCode>C001</CutomerCode>
    <CustomerName>Shahbaz</CustomerName>
    <Address>
      <City>Karachi</City>
      <Country>Pakistan</Country>
    </Address>
    <CutomerCode>C002</CutomerCode>
    <CustomerName>Imran</CustomerName>
    <Address>
      <City>Lahore</City>
      <Couontry>Pakistan</Couontry>
    </Address>
  </Customers>
</MyCustomer>

请帮助我想要正确转换。请帮助我想要正确的xslt方法请帮助

1 个答案:

答案 0 :(得分:1)

您确定要打开个人Customers吗?

由于这两个模板,您的样式表似乎无法正常工作:

<xsl:template match="Customers/City">
      <xsl:element name="Address">
         <xsl:element name="City">
       <xsl:value-of select="." />
        </xsl:element>
    </xsl:element>
 </xsl:template>
 <xsl:template match="Customers/Country">
      <xsl:element name="Address">
         <xsl:element name="Country">
       <xsl:value-of select="." />
        </xsl:element>
    </xsl:element>
 </xsl:template>

这些为您提供了两个不同的Address元素。

可以做类似的事情:

<xsl:template match="Customers/City">
    <Address>
        <City>
            <xsl:value-of select="." />
        </City>
        <Country>
            <xsl:value-of select="following-sibling::Country"/>
        </Country>
    </Address>
</xsl:template>
<xsl:template match="Customers/Country"/>

这是一个类似的XSLT 1.0样式表,它提供了想要的结果,在我看来更清洁一点:

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

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

    <xsl:template match="Customers">
        <xsl:apply-templates select="node()[not(self::City) and not(self::Country)]"/>
        <Address>
            <xsl:apply-templates select="City|Country"/>
        </Address>
    </xsl:template>

</xsl:stylesheet>

XML输出

<MyCustomer>
   <Customers>
      <CutomerCode>C001</CutomerCode>
      <CustomerName>Shahbaz</CustomerName>
      <Address>
         <City>Karachi</City>
         <Country>Pakistan</Country>
      </Address>
      <CutomerCode>C002</CutomerCode>
      <CustomerName>Imran</CustomerName>
      <Address>
         <City>Lahore</City>
         <Country>Pakistan</Country>
      </Address>
   </Customers>
</MyCustomer>