所以我有一些XML,我想要相同的XML,但缺少几行。这是我的原始XML:
<?xml version="1.0" encoding="UTF-8"?>
<Customers>
<Customer>
<FirstName>Sarah</FirstName>
<LastName>Bellum</LastName>
<PhoneNumbers>
<PhoneNumber type="HOME">
223 704 2215
</PhoneNumber>
<PhoneNumber type="WORK">
223 704 1234
</PhoneNumber>
<PhoneNumber type="CELL">
860 704 1234
</PhoneNumber>
</PhoneNumbers>
<Addresses>
<Address type="HOME">
<Line1>123 Main</Line1>
<Line2></Line2>
<Line3></Line3>
<City>Wallingford</City>
<State>CT</State>
<Zip>12345</Zip>
</Address>
<Address type="WOKR">
<Line1>456 Main</Line1>
<Line2></Line2>
<Line3></Line3>
<City>Willington</City>
<State>CT</State>
<Zip>12345</Zip>
</Address>
</Addresses>
</Customer>
<Customer>
<FirstName>Mike</FirstName>
<LastName>Easter</LastName>
<PhoneNumbers>
<PhoneNumber type="CELL">
123 704 4321
</PhoneNumber>
</PhoneNumbers>
<Addresses>
<Address type="HOME">
<Line1>14 East</Line1>
<Line2></Line2>
<Line3></Line3>
<City>Shelton</City>
<State>CT</State>
<Zip>12345</Zip>
</Address>
</Addresses>
</Customer>
</Customers>
这是我的目标XML:
<Customers>
<Customer>
<Name>Sarah Bellum</Name>
<!-- This element should only appear if the source element exists -->
<HomePhone>223 704 2215</HomePhone>
<!-- This element should only appear if the source element exists -->
<WorkPhone>223 704 1234</WorkPhone>
<!-- This element should only appear if the source element exists -->
<CellPhone>860 704 1234</CellPhone>
<!-- This element should only appear if the source element exists -->
<HomeAddress>
<Street>123 Main</Street>
<City>Wallingford</City>
<State>CT</State>
<Zip>12345</Zip>
</HomeAddress>
<!-- This element should only appear if the source element exists -->
<WorkAddress>
<Street>456 Main</Street>
<City>Willington</City>
<State>CT</State>
<Zip>12345</Zip>
</WorkAddress>
</Customer>
<Customer>
<Name>Mike Easter</Name>
<CellPhone>123 704 4321</CellPhone>
<HomeAddress>
<Street>14 East</Street>
<City>Shelton</City>
<State>CT</State>
<Zip>12345</Zip>
</HomeAddress>
</Customer>
</Customers>
我有一些XSLT,但它只是复制了它,我想删除一些行。这是我的XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
因此,我为您提供了目标XML,原始XML和一些XSL。我不知道如何摆脱限制,所以请您帮忙。
答案 0 :(得分:1)
例如,这里的一些转换规则很简单
<xsl:template match="FirstName">
<Name><xsl:value-of select="concat(., ' ', ../LastName)"/>
</xsl:template>
<xsl:template match="LastName"/>
或
<xsl:template match="Phone[@type='CELL']">
<CellPhone><xsl:value-of select="."/></CellPhone>
</xsl:template>
其他人更加困难;我不知道您如何确定输入地址的哪一行是街道。
但是基本上,您有什么问题?您进行此转换所面临的困难到底是什么?从表面上看,这里没有任何“ Beginning XSLT”教科书没有涵盖的内容。