Inside XSLT I have state which is coming from XML and on corresponding to that state I have my own state code which I want to put in transformed XML.
在XSLT中,我有来自XML的状态,并且对应于该状态,我有自己的状态代码,我希望将其放入转换后的XML中。
Source XML:
源XML:
<states>
<state>New York</state>
<state>California</state>
</states>
Expected Result:
预期结果:
<states>
<state>NY</state>
<state>CA</state>
</states>
As I have list of states so I can't use when or If statement so Is their any optimized way to solve this problem in XSLT 1.0 or 2.0? Thanks in advance.
因为我有状态列表所以我不能使用when或If语句这样是他们在XSLT 1.0或2.0中解决这个问题的任何优化方法吗?提前致谢。
2 个解决方案
#1
0
Here's one way you could look at it:
这是你可以看到它的一种方式:
XSLT 2.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>
If you prefer, you could place the list of states and their codes in an external XML document and lookup from there.
如果您愿意,可以将状态列表及其代码放在外部XML文档中,然后从那里查找。
As I have list of states so I can't use when or If statement
因为我有状态列表所以我不能使用when或If语句
Actually, using xsl:choose
could work just as well.
实际上,使用xsl:choose也可以正常工作。
#2
0
You can include the external file with the mappings and access the mappings. I used 'b1.xml' as the filename. It replaces the full-names with the short-name.
您可以将外部文件包含在映射中并访问映射。我使用'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>
The external mapping file, which you should replace with your own, is:
外部映射文件,您应该用自己的替换,是:
<?xml version="1.0"?>
<StateMapping>
<Map name="New York">NY</Map>
<Map name="California">CA</Map>
</StateMapping>
Output is:
输出是:
<?xml version="1.0"?>
<states>
<state>NY</state>
<state>CA</state>
</states>
#1
0
Here's one way you could look at it:
这是你可以看到它的一种方式:
XSLT 2.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>
If you prefer, you could place the list of states and their codes in an external XML document and lookup from there.
如果您愿意,可以将状态列表及其代码放在外部XML文档中,然后从那里查找。
As I have list of states so I can't use when or If statement
因为我有状态列表所以我不能使用when或If语句
Actually, using xsl:choose
could work just as well.
实际上,使用xsl:choose也可以正常工作。
#2
0
You can include the external file with the mappings and access the mappings. I used 'b1.xml' as the filename. It replaces the full-names with the short-name.
您可以将外部文件包含在映射中并访问映射。我使用'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>
The external mapping file, which you should replace with your own, is:
外部映射文件,您应该用自己的替换,是:
<?xml version="1.0"?>
<StateMapping>
<Map name="New York">NY</Map>
<Map name="California">CA</Map>
</StateMapping>
Output is:
输出是:
<?xml version="1.0"?>
<states>
<state>NY</state>
<state>CA</state>
</states>