简体   繁体   English

XSLT:将xml内容拆分为单独的元素

[英]XSLT:Split the xml content into separate elements

I have the following xml. 我有以下xml。

 <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="applyt.xsl" ?>
    <customers>
      <order>
        <id>1</id>
        <name>John</name>
        <customerBlkNo>178</customerBlkNo>
        <CustomerStreetNo>xyz Avenue 1</CustomerStreetNo>
        <CustomerCountry>China</CustomerCountry>
        <phone>123-4567</phone>
       </order>
    </customers>

I need to transform this xml as shown below. 我需要如下所示转换此xml。

 <customers>
      <order>
        <id>1</id>
        <name>John</name>
        <customeraddress>
          <BlkNo>178</BlkNo>
          <StreetNo>xyz Avenue 1</StreetNo>
          <Country>China</Country>
        </customeraddress>
        <phone>123-4567</phone>
       </order>
  </customers>

I am new to xslt. 我是xslt的新手。 Could someone please help me.Thanks in advance 有人可以帮我吗,谢谢

Because XSLT is so much fun, here is a working stylesheet that summarizes the customer address. 因为XSLT非常有趣,所以这里是一个工作样式表,它总结了客户地址。 It uses XSLT 2.0 since you did not say which version you'd like to use. 它使用XSLT 2.0,因为您没有说出要使用哪个版本。

Stylesheet 样式表

<?xml version="1.0" encoding="utf-8"?>

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

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

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="customers|order|id|name|phone">
  <xsl:copy>
     <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="customerBlkNo">
  <customeraddress>
     <BlkNo>
        <xsl:value-of select="."/>
     </BlkNo>
     <StreetNo>
        <xsl:value-of select="../CustomerStreetNo"/>
     </StreetNo>
     <Country>
        <xsl:value-of select="../CustomerCountry"/>
     </Country>
  </customeraddress>
</xsl:template>

<xsl:template match="CustomerStreetNo|CustomerCountry"/>

</xsl:stylesheet>

Output 产量

<?xml version="1.0" encoding="UTF-8"?>
<customers>
 <order>
  <id>1</id>
  <name>John</name>
  <customeraddress>
     <BlkNo>178</BlkNo>
     <StreetNo>xyz Avenue 1</StreetNo>
     <Country>China</Country>
  </customeraddress>
  <phone>123-4567</phone>
 </order>
</customers>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM