除了子属性之外,还循环遍历所有父属性

时间:2021-06-29 02:34:33

标签: xml xslt xslt-2.0

我创建了一个 XSLT 转换来将以下 XML 转换为 CSV

<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Package ID="1122" BusinessID="001233" Version="19.0.2" xsi:type="Doms">
    <Name>ABC</Name>
    <Description> Desc sample</Description>
    <Category>54</Category>
    <Business_ID>001233</Business_ID>

    <TPFC ID="76" xsi:type="TPFC" Pattern="Th_Branch">
        <Name>AM</Name>
        <Parent ID="11d" xsi:type="TPFC" Pattern="Th_Branch">
            <Description>Mob1</Description>
            <Name>Mob1</Name>
            <Parent ID="12F" xsi:type="TPFC" Pattern="Th_Branch">
            <Description>Mob2</Description>
            <Name>Mob2</Name>
            </Parent>
        </Parent>
    </TPFC>
</Package>

下面是我创建的用于遍历节点的 XSLT

    <xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8"/>
    <!-- Delimiter -->
    <xsl:variable name="separator" select="'|'"/>
    <!-- New line character -->
    <xsl:variable name="newline" select="'&#10;'"/>
<xsl:template match="/Root">
    <!-- header -->
    <xsl:text>id|name|description|parent_name|parent_id|level|total_level|product_offering_id</xsl:text>
<xsl:value-of select="$newline"/>
    <!-- data -->
    <xsl:for-each select="Package">
        <xsl:variable name="b-id" select="Business_ID" />
        <!-- TPFC data -->
        <xsl:for-each select="TPFC ">
          <xsl:value-of select="translate(Name,' ',''), .//Parent/Name" separator="_"/>
            <xsl:value-of select="$separator"/>
            <xsl:value-of select="Name"/>
            <xsl:value-of select="$separator"/>
            <xsl:value-of select="Description"/>
    
<xsl:value-of select="$newline"/>
          <xsl:for-each select="//Parent">
            <xsl:value-of select="translate(Name,' ',''), .//Parent/Name" separator="_"/>
            <xsl:value-of select="$separator"/>
            <xsl:value-of select="Name"/>
            <xsl:value-of select="$separator"/>
            <xsl:value-of select="Description"/>
<xsl:value-of select="$newline"/>
       </xsl:for-each>
        </xsl:for-each>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each> 
</xsl:template>
</xsl:stylesheet>

当前结果是一个 CSV,其中为 TPFC 中的父节点和子节点创建了一条记录。

 | id           | name | description | 
  | AM_Mob1_Mob2 | AM   |             | 
  | Mob1_Mob2    | Mob1 |             | 
  | Mob2         | Mob2 | Mob 2       | 

是否有其他更好的方法来迭代具有相同 xsi:type=TPFC 的所有节点,或者是否有一种方法可以重用在两个 for-each 中重复使用的 xsl select 语句声明

0 个答案:

没有答案
相关问题