簡體   English   中英

如何在XSLT中創建鍵值對並使用鍵獲取值

[英]How to create key value pair inside XSLT and get value using key

在XSLT中,我有來自XML的狀態,並且對應於該狀態,我有自己的狀態代碼,我希望將其放入轉換后的XML中。

源XML:

<states>
<state>New York</state>
<state>California</state>
</states>

預期結果:

<states>
    <state>NY</state>
    <state>CA</state>
</states>

因為我有狀態列表所以我不能使用whenIf語句所以它們是否有任何優化的方法來解決XSLT 1.0或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>

如果您願意,可以將狀態列表及其代碼放在外部XML文檔中,然后從那里查找。

由於我有狀態列表所以我不能使用when或If語句

實際上,使用xsl:choose也可以正常工作。

您可以將外部文件包含在映射中並訪問映射。 我使用'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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM