使用 XSLT 将 XML 文件转换为另一个 XML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6002772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:42:39  来源:igfitidea点击:

Converting XML file to another XML file using XSLT

xmlxslt

提问by shavinda

XML file 1:

XML 文件 1:

<?xml version="1.0"?>
<rentalProperties>
    <property contact ="1">
        <type>House </type>
        <price>420</price>
        <address>
            <streetNo>1</streetNo>
            <street>Wavell Street</street>
            <suburb>Box Hill</suburb>
            <state>VIC</state>
            <zipcode>3128</zipcode> 
        </address>
        <numberOfBedrooms>3</numberOfBedrooms>
        <numberOfBathrooms>1</numberOfBathrooms> 
        <garage>1</garage>   
    </property>

XML file 2:

XML 文件 2:

<?xml version="1.0"?>
<rentalProperties>
    <property contact ="1">
        <type>House </type>
        <price>420</price>
        <address>1 wavell street,Box Hill,VIC,Australia</address>
        <numberOfBedrooms>3</numberOfBedrooms>
        <numberOfBathrooms>1</numberOfBathrooms> 
        <garage>1</garage>     
    </property>

How should i convert xml file 1 to xml fle 2 using xslt? i want to represent the address as the single line and add a new attribute [country- Australia] to end of the line. i did the rest of it . i'm struggling with address line

我应该如何使用 xslt 将 xml 文件 1 转换为 xml 文件 2?我想将地址表示为单行,并在行尾添加一个新属性 [country-Australia]。我做了剩下的。我正在为地址行苦苦挣扎

XSLT file:

XSLT 文件:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" type="text/css" href="style.css">
    <xsl:template match="/">
        <rentalProperties>
            <property>
                <xsl:attribute name="contact"><xsl:value-of select='@contact'/></xsl:attribute>    
                <type><xsl:value-of select="type"/></type>
                <price><xsl:value-of select="price"/></price>
                <numberOfBedrooms><xsl:value-of select="numberOfBedrooms"/></numberOfBedrooms>
                <numberOfBathrooms><xsl:value-of select="numberOfBathrooms"/></numberOfBathrooms>
                <garage><xsl:value-of select="garage"/></garage>    
            </property>    
        </rentalProperties>    
    </xsl:template>
</xsl:stylesheet>

回答by Dimitre Novatchev

This transformation:

这种转变

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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="address">
  <xsl:copy>
   <xsl:value-of select=
   "concat(streetNo, ' ', street, ',',
           suburb,',', state,', Australia')
   "/>
  </xsl:copy>
 </xsl:template>
 <xsl:template match="address/node()"/>
</xsl:stylesheet>

when applied on the provided XML document:

当应用于提供的 XML 文档时

<rentalProperties>
    <property contact ="1">
        <type>House </type>
        <price>420</price>
        <address>
            <streetNo>1</streetNo>
            <street>Wavell Street</street>
            <suburb>Box Hill</suburb>
            <state>VIC</state>
            <zipcode>3128</zipcode>
        </address>
        <numberOfBedrooms>3</numberOfBedrooms>
        <numberOfBathrooms>1</numberOfBathrooms>
        <garage>1</garage>
    </property>
</rentalProperties>

produces the wanted, correct result:

产生想要的、正确的结果

<rentalProperties>
   <property contact="1">
      <type>House </type>
      <price>420</price>
      <address>1 Wavell Street,Box Hill,VIC, Australia</address>
      <numberOfBedrooms>3</numberOfBedrooms>
      <numberOfBathrooms>1</numberOfBathrooms>
      <garage>1</garage>
   </property>
</rentalProperties>

Explanation: Using and overriding the identity rule.

说明:使用和覆盖身份规则

回答by Osiris76

You could introduce a new template for the address block using

您可以使用为地址块引入一个新模板

<xsl:template match="address">
    <xsl:value-of select="streetNo" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="street" />
    <xsl:text>,</xsl:text>
    <xsl:value-of select="suburb" />
    <xsl:text>,</xsl:text>
    <xsl:value-of select="state" />
    <xsl:text>,</xsl:text>
    <xsl:value-of select="zipcode" />
</xsl:template>

and call it with

并调用它

<xsl:apply-templates select="address" />

before the <numberOfBedrooms>element. This can also be done using the concatfunction, whereas the correct syntax I don't remember right now.

<numberOfBedrooms>元素之前。这也可以使用该concat函数来完成,而我现在不记得正确的语法。

回答by actionshrimp

You can try something like:

您可以尝试以下操作:

<address>
    <xsl:for-each select="address/*">
       <xsl:value-of select="."/>, 
    </xsl:for-each>
    Australia
</address>

This loops over all the children of the address tag in xml1.

这将遍历 xml1 中地址标记的所有子项。

回答by raj

<rentalProperties>
    <property contact="1">
        <type>House </type>
        <price>420</price>
        <address>1 Wavell Street,Box Hill,VIC,3128</address>
        <numberOfBedrooms>3</numberOfBedrooms>
        <numberOfBathrooms>1</numberOfBathrooms> 
        <garage>1</garage>   
    </property>
</rentalProperties>