使用 XSLT 将 XML 文件转换为另一个 XML 文件
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 文件 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>
我应该如何使用 xslt 将 xml 文件 1 转换为 xml 文件 2? 我想将地址表示为单行,并在行尾添加一个新属性 [国家/地区-澳大利亚]。剩下的我都做了。我正在努力处理地址行
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>
XML file 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 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 file:
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此转换:
应用于提供的 XML 文档时:
产生所需的正确结果:
说明:使用和覆盖身份规则。
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation: Using and overriding the identity rule.
您可以为地址块引入一个新模板,
调用它
并在
元素之前 。这也可以使用 concat 函数来完成,但我现在不记得正确的语法。You could introduce a new template for the address block using
and call it with
before the
<numberOfBedrooms>
element. This can also be done using theconcat
function, whereas the correct syntax I don't remember right now.您可以尝试类似的操作:
这将循环遍历 xml1 中地址标记的所有子级。
You can try something like:
This loops over all the children of the address tag in xml1.