通过交叉引用跨xml中的元素进行XSL元素属性查找

时间:2015-04-17 21:52:04

标签: xml xslt xpath

xslt相当新,并且希望能够在遍历xpath时在其他元素中执行属性值查找。 我有一个xml我需要处理并从中获取值。基本上我想得到一个属性值并找到包含该值的元素并抓住底层子元素。

我的xml结构是这样的:

<root>
<first>
    <second>
        <third>  
            <county>
                 <districts>
                                <district>
                                    <cityTaxCode zip="01234" />
                                    <cityTaxCode zip="56789" />
                                </district>
                        </districts>
                    </county>

            <schoolDistricts>
                        <schoolTaxId zipid="01234">
                                <locality>
                                    upper
                                </locality>
                                <county>
                                    <value>east-highland</value>
                                </county>
                        </schoolTaxId>
                        <schoolTaxId zipid="56789">
                            <locality>
                                    lower
                                </locality>
                            <county>
                                    <value>west-highland</value>
                            </county>
                        </schoolTaxId>
                    </schoolDistricts>
        </third>
    </second>   
</first>
</root>

我希望的输出是:

County

 - district | zip: 01234 | east-highland
 - district | zip: 56789 | west-highland

我尝试使用xsl键元素查找,但每次运行时,我都会得到这样的空白结果:

County

 - district 
 - district 

我的xslt看起来像这样:

 <xsl:key name="zipLookup" match="root/first/second/third/schoolDistricts/schoolTaxId" 
use="@zipid"/>

 <xsl:template match="root/first/second/third/county/districts/district/cityTaxCode" >

<xsl:element name="County">
    County  

        <xsl:for-each select="key('zipLookup', @zip)">      
            <xsl:apply-templates select="*/county" />
        </xsl:for-each>
    </xsl:element>        
</xsl:template>


<xsl:template match="county">   
     <xsl:element name="Value">
        <xsl:value-of select="@value"/>
     </xsl:element>
</xsl:template>

出于某种原因,它一直有效,直到它无法找到另一个&#34;县&#34;兄弟元素中的元素,它们都是第三个元素节点的子元素。任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

您显示文本输出,但样式表生成XML - 这有点令人困惑。

查看以下样式表是否有帮助:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="zipLookup" match="schoolTaxId" use="@zipid"/>

<xsl:template match="/root" >
    <root>
        <xsl:apply-templates select="first/second/third/county/districts/district/cityTaxCode" />
    </root>        
</xsl:template>

<xsl:template match="cityTaxCode" >
    <zone zip="{@zip}">
        <xsl:apply-templates select="key('zipLookup', @zip)" />
    </zone>        
</xsl:template>

<xsl:template match="schoolTaxId">   
     <county>
        <xsl:value-of select="county/value"/>
     </county>
</xsl:template>

</xsl:stylesheet>

应用于您的示例输入(在更正拼写错误后)产生:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <zone zip="01234">
      <county>east-highland</county>
   </zone>
   <zone zip="56789">
      <county>west-highland</county>
   </zone>
</root>
相关问题