xml - 创建 XSL 文件以提取子元素的值

标签 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>

我需要从 wd:Location/wd:ID wd:type="Location_ID"读取值“Wichita Branch”。
<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)"/>

上面的一个将“faf237f827f810890f85c091c1fe09cc”与“Wichita Branch”连接起来。下面的三个返回一个空值。
<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 文件的基本格式以开始使用。我会很感激帮助。谢谢你。

最佳答案

看起来您的选择属性中只是有一个小错字。您需要添加 wd type 的命名空间前缀属性,所以它变成 @wd:type .

您还应该能够删除 text()来自 XPath,因为 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 的 child .

关于xml - 创建 XSL 文件以提取子元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47682703/

相关文章:

xml - 将 Excel 电子表格导出到 XML 并在导出期间或之后按字母顺序对数组进行排序?

html - XSLT 允许不匹配的标签?

shell - Scrapy:在 shell 中使用xpath检索到的数据,但未在项中检索

html - 在 xslt 文件中获取 "prefix ' soap' is not defined"

xml - 在单个 XPath 表达式中获取恰好引用两个其他元素的元素

c# - Xpath 通配符(例如 : "/Data/Customers/D*")

java - JaxB 验证事件定位器 - 错误的对象引用

sql-server - 工作流网站 - 后端设计建议

xml - raku 语法的标记不会命中文档的第一次出现,但会命中类似的后续出现

java - 如何将单个 java bean 编码到具有现有注释的复杂 XML 文档中?