创建XSL文件以获取子元素的值

时间:2017-12-06 20:12:42

标签: xml xslt xpath

我有这个XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<wd:Report_Data xmlns:wd="urn:com.workday.report/xxxxxxxxxxxxxxxxx">
  <wd:Report_Entry>
    <wd:EmployerName>ABC Company</wd:EmployerName>
    <wd:EmployeeID>123456789</wd:EmployeeID>
    <wd:FirstName>Jane</wd:FirstName>
    <wd:LastName>Doe</wd:LastName>
    <wd:Email>jane.l.doe@emcins.com</wd:Email>
    <wd:SSN>111-22-3333</wd:SSN>
    <wd:PostalCode>54321</wd:PostalCode>
    <wd:Division>0</wd:Division>
    <wd:Group>Eligible</wd:Group>
    <wd:Country>0</wd:Country>
    <wd:HealthPlan>Non-Health Plan User</wd:HealthPlan>
    <wd:Location wd:Descriptor="Wichita Branch">
        <wd:ID wd:type="WID">zdf238f821r818890f54c9324ee609db</wd:ID>
        <wd:ID wd:type="Location_ID">Wichita Branch</wd:ID>
    </wd:Location>
    <wd:Region wd:Descriptor="Married (United States of America)">
        <wd:ID wd:type="WID">faf237f571f81088909963455816061f</wd:ID>
        <wd:ID wd:type="Marital_Status_ID">Married_United_States_of_America</wd:ID>
    </wd:Region>
    <wd:Store>2017-01-09-08:00</wd:Store>
    <wd:AgeRange>22</wd:AgeRange>
    <wd:BargainingUnit>Established</wd:BargainingUnit>
    <wd:Class wd:Descriptor="Salary">
        <wd:ID wd:type="WID">faf237f471f81567d35c25d274eef713</wd:ID>
        <wd:ID wd:type="Pay_Rate_Type_ID">Salary</wd:ID>
    </wd:Class>
    <wd:CurrentWalking>0</wd:CurrentWalking>
    <wd:Department wd:Descriptor="Human Resources - General">
        <wd:ID wd:type="WID">faf237f671f41589c20f6681d436e539</wd:ID>
        <wd:ID wd:type="Organization_Reference_ID">Human Resources - General</wd:ID>
        <wd:ID wd:type="Cost_Center_Reference_ID">Human Resources - General</wd:ID>
    </wd:Department>
    <wd:Gender wd:Descriptor="Female">
        <wd:ID wd:type="WID">faf571h561f810889012149c7346059p</wd:ID>
        <wd:ID wd:type="Gender_Code">Female</wd:ID>
    </wd:Gender>
    <wd:Status wd:Descriptor="Regular">
        <wd:ID wd:type="WID">fac242f671f8108555784cb0357608cc</wd:ID>
        <wd:ID wd:type="Employee_Type_ID">Regular</wd:ID>
    </wd:Status>
    <wd:BirthDate>1995-02-17-07:00</wd:BirthDate>
    <wd:RelationshipCode>0</wd:RelationshipCode>
    <wd:Address1 wd:Descriptor="1115 Bellflower Dr">
        <wd:ID wd:type="WID">c70dbf571214999732564dah2m1358f8</wd:ID>
    </wd:Address1>
    <wd:City>Des Moines</wd:City>
    <wd:State wd:Descriptor="Iowa">
        <wd:ID wd:type="WID">a9be12393ccd692ea44d58c678ce6c8b</wd:ID>
        <wd:ID wd:type="Country_Region_ID">USA-IA</wd:ID>
        <wd:ID wd:type="ISO_3166-2_Code">IA</wd:ID>
    </wd:State>
    <wd:spouseSId>s123456789</wd:spouseSId>
    <wd:spouseSFirstName>Tom</wd:spouseSFirstName>
    <wd:spouseSLastName>Doe</wd:spouseSLastName>
    <wd:spouseSDob>1990-05-12-08:00</wd:spouseSDob>
    <wd:spouseSPreferredEmail>tdoe@gmail.com</wd:spouseSPreferredEmail>
</wd:Report_Entry>
</wd:Report_Data>

我创建了这个XSL文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wd="urn:com.workday.report/xxxxxxxxxxxxxxxxx" exclude-result-prefixes="xs"
version="2.0" xmlns:functx="http://www.functx.com">
<xsl:output method="text"/>
<xsl:param name="separator" select="','"/>
<xsl:param name="padding" select="' '"/>
<xsl:param name="spouse" select="'S'"/>
<xsl:variable name="allowednum" select="'0123456789-'"/>
<xsl:variable name="dateFormat" select="'[M01]/[D01]/[Y0001]'"/>
<xsl:variable name="spaces" select="concat('          ','          ','          ','          ','          ','          ','          ','          ','          ','          ','          ','          ','          ','          ')"/>
<xsl:template match="wd:Report_Data">
    <xsl:for-each select="wd:Report_Entry">
        <xsl:value-of select="substring(concat(wd:EmployerName,$spaces),1,13)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:EmployeeID,$spaces),1,9)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:FirstName,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:LastName,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Email,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:SSN,$spaces),1,11)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:PostalCode,$spaces),1,5)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:JobCode,$spaces),1,9)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Division,$spaces),1,9)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Group,$spaces),1,9)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Country,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:HealthPlan,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Location/wd:ID[@type='Location_ID']/text(),$spaces),1,50)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Region,$spaces),1,50)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Store,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:AgeRange,$spaces),1,3)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:BargainingUnit,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Class,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:CurrentWalking,$spaces),1,9)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Department,$spaces),1,50)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:District,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Facility,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Gender,$spaces),1,6)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Status,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:BirthDate,$spaces),1,10)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:TerminationDate,$spaces),1,10)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:RelationshipCode,$spaces),1,1)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:SubscriberID,$spaces),1,9)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Address1,$spaces),1,50)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:Address2,$spaces),1,50)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:City,$spaces),1,50)"/>
        <xsl:value-of select="$separator"/>
        <xsl:value-of select="substring(concat(wd:State,$spaces),1,30)"/>
        <xsl:value-of select="$separator"/>
        <xsl:choose>
            <xsl:when test="wd:spouseSFirstName != ''">
                <xsl:text>&#xa;</xsl:text>
                <xsl:value-of select="substring(concat(wd:EmployerName,$spaces),1,13)"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="substring(concat($spouse,wd:EmployeeID,$spaces),1,10)"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="substring(concat(wd:spouseSFirstName,$spaces),1,30)"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="substring(concat(wd:spouseSLastName,$spaces),1,30)"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="substring(concat(wd:spouseSPreferredEmail,$spaces),1,30)"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>     
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>
                <xsl:value-of select="$separator"/>                     
                <xsl:value-of select="substring(concat(wd:spouseSDob,$spaces),1,10)"/>
                <xsl:value-of select="$separator"/>
            </xsl:when>
            <xsl:otherwise>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

我需要阅读价值&#34; Wichita Branch&#34;来自wd:Location / wd:ID wd:type =&#34; Location_ID&#34;。

<wd:Location wd:Descriptor="Wichita Branch">
        <wd:ID wd:type="WID">zdf238f821r818890f54c9324ee609db</wd:ID>
        <wd:ID wd:type="Location_ID">Wichita Branch</wd:ID>
    </wd:Location>

我试过以下但没有运气:

<xsl:value-of select="substring(concat(wd:Location,$spaces),1,50)"/>

上面的一个联合&#34; faf237f827f810890f85c091c1fe09cc&#34;与&#34; Wichita Branch&#34;。下面的三个返回一个空值。

<xsl:value-of select="substring(concat(wd:Location/ID[@type='Location_ID',$spaces),1,50)"/>

<xsl:value-of select="substring(concat(wd:Location/wd:ID[@type='Location_ID'],$spaces),1,50)"/>

<xsl:value-of select="substring(concat(wd:Location/wd:ID[@type='Location_ID']/text(),$spaces),1,50)"/>

我是创建XSL文件的新手,并且承认我从我公司创建的另一个XSL文件中复制了XSL文件的基本格式以开始使用。我很感激帮助。谢谢。

2 个答案:

答案 0 :(得分:0)

这将选择“Location_ID”类型的所有ID的所有文本

<xsl:value-of select="//wd:Location/wd:ID[@type = 'Location_ID')/text()"/>

然而@Haley评论说你应该考虑评估选择表达式的当前节点

答案 1 :(得分:0)

看起来你的select属性中只有一个小错字。您需要将wd命名空间前缀添加到type属性,使其变为@wd:type

您还应该能够从XPath中删除text(),因为wd:ID中没有任何子元素。

尝试更改:

<xsl:value-of select="substring(concat(wd:Location/wd:ID[@type='Location_ID']/text(),$spaces),1,50)"/>

为:

<xsl:value-of select="substring(concat(wd:Location/wd:ID[@wd:type='Location_ID'],$spaces),1,50)"/>

另外,如果您在文本输出中出现奇怪的换行符,请尝试添加:

<xsl:strip-space elements="*"/>

作为xsl:stylesheet的孩子。

相关问题