在XSLT中,我有来自XML的状态,并且对应于该状态,我有自己的状态代码,我希望将其放入转换后的XML中。
源XML:
<states>
<state>New York</state>
<state>California</state>
</states>
预期结果:
<states>
<state>NY</state>
<state>CA</state>
</states>
由于我有州列表所以我不能使用 或如果语句,那么他们是否有任何优化的方法来解决XSLT 1.0或2.0中的这个问题?提前谢谢。
答案 0 :(得分:0)
这是你可以看到它的一种方式:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://example.com/my">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="state" match="state" use="." />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="state">
<xsl:copy>
<xsl:value-of select="key('state', ., document(''))/@code"/>
</xsl:copy>
</xsl:template>
<my:states>
<state code="AL">Alabama</state>
<state code="AK">Alaska</state>
<state code="AZ">Arizona</state>
<state code="AR">Arkansas</state>
<state code="CA">California</state>
<state code="CO">Colorado</state>
<state code="CT">Connecticut</state>
<state code="DE">Delaware</state>
<state code="DC">District of Columbia</state>
<state code="FL">Florida</state>
<state code="GA">Georgia</state>
<state code="HI">Hawaii</state>
<state code="ID">Idaho</state>
<state code="IL">Illinois</state>
<state code="IN">Indiana</state>
<state code="IA">Iowa</state>
<state code="KS">Kansas</state>
<state code="KY">Kentucky</state>
<state code="LA">Louisiana</state>
<state code="ME">Maine</state>
<state code="MD">Maryland</state>
<state code="MA">Massachusetts</state>
<state code="MI">Michigan</state>
<state code="MN">Minnesota</state>
<state code="MS">Mississippi</state>
<state code="MO">Missouri</state>
<state code="MT">Montana</state>
<state code="NE">Nebraska</state>
<state code="NV">Nevada</state>
<state code="NH">New Hampshire</state>
<state code="NJ">New Jersey</state>
<state code="NM">New Mexico</state>
<state code="NY">New York</state>
<state code="NC">North Carolina</state>
<state code="ND">North Dakota</state>
<state code="OH">Ohio</state>
<state code="OK">Oklahoma</state>
<state code="OR">Oregon</state>
<state code="PA">Pennsylvania</state>
<state code="RI">Rhode Island</state>
<state code="SC">South Carolina</state>
<state code="SD">South Dakota</state>
<state code="TN">Tennessee</state>
<state code="TX">Texas</state>
<state code="UT">Utah</state>
<state code="VT">Vermont</state>
<state code="VA">Virginia</state>
<state code="WA">Washington</state>
<state code="WV">West Virginia</state>
<state code="WI">Wisconsin</state>
<state code="WY">Wyoming</state>
</my:states>
</xsl:stylesheet>
如果您愿意,可以将状态列表及其代码放在外部XML文档中,然后从那里查找。
因为我有状态列表所以我不能使用when或If语句
实际上,使用xsl:choose
也可以正常工作。
答案 1 :(得分:0)
您可以将外部文件包含在映射中并访问映射。我使用'b1.xml'作为文件名。它用短名称替换全名。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="StateMap" select="document('b1.xml')/StateMapping" />
<xsl:template match="states">
<states>
<xsl:apply-templates select="state" />
</states>
</xsl:template>
<xsl:template match="state">
<xsl:variable name="toMap" select="text()" />
<state><xsl:value-of select="$StateMap/Map[@name = $toMap]" /></state>
</xsl:template>
</xsl:stylesheet>
外部映射文件应该替换为您自己的文件:
<?xml version="1.0"?>
<StateMapping>
<Map name="New York">NY</Map>
<Map name="California">CA</Map>
</StateMapping>
输出为:
<?xml version="1.0"?>
<states>
<state>NY</state>
<state>CA</state>
</states>