2

我有一个用于绑定的 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 方法请帮助

4

1 回答 1

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>
于 2013-02-08T06:48:13.970 回答