格式化xml的xsl

时间:2015-03-05 02:19:57

标签: xml xslt

我试图让我的xsl文件恰到好处,但我遇到了一些问题。这是我正在使用的xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<ExportedData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Header>
    <StartDate>2015-02-02T00:00:00</StartDate>
    <EndDate>2015-03-04T23:59:00</EndDate>
    <RecordCount>4</RecordCount>
    <Client />
    <DocumentCount>0</DocumentCount>
  </Header>
  <Applicants>
    <Applicant>
      <ApplicantId>2176</ApplicantId>
      <ModuleTypeId>1</ModuleTypeId>
      <ApplicantInfo>
        <Applications>
          <Application>
            <ApplicationId>6177</ApplicationId>
            <Fields>
              <Field>
                <FieldName>Action Status</FieldName>
                <FieldText>Submitted</FieldText>
              </Field>
              <Field>
                <FieldName>BGCheck Result</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Date Hired</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Location Code</FieldName>
                <FieldText>ManNY</FieldText>
              </Field>
            </Fields>
          </Application>
        </Applications>
      </ApplicantInfo>
      <ApplicantActionDocsInfo />
      <ApplicantFormsInfo />
      <ApplicantActionsInfo />
    </Applicant>
    <Applicant>
      <ApplicantId>2180</ApplicantId>
      <ModuleTypeId>1</ModuleTypeId>
      <ApplicantInfo>
        <Applications>
          <Application>
            <ApplicationId>6181</ApplicationId>
            <Fields>
              <Field>
                <FieldName>Action Status</FieldName>
                <FieldText>Submitted</FieldText>
              </Field>
              <Field>
                <FieldName>BGCheck Result</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Date Hired</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Location Code</FieldName>
                <FieldText>Test0701</FieldText>
              </Field>
            </Fields>
          </Application>
        </Applications>
      </ApplicantInfo>
      <ApplicantActionDocsInfo />
      <ApplicantFormsInfo />
      <ApplicantActionsInfo />
    </Applicant>
    <Applicant>
      <ApplicantId>2193</ApplicantId>
      <ModuleTypeId>1</ModuleTypeId>
      <ApplicantInfo>
        <Applications>
          <Application>
            <ApplicationId>6160</ApplicationId>
            <Fields>
              <Field>
                <FieldName>Action Status</FieldName>
                <FieldText>Submitted</FieldText>
              </Field>
              <Field>
                <FieldName>BGCheck Result</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Date Hired</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Location Code</FieldName>
                <FieldText>00 Demo</FieldText>
              </Field>
            </Fields>
          </Application>
          <Application>
            <ApplicationId>6170</ApplicationId>
            <Fields>
              <Field>
                <FieldName>Action Status</FieldName>
                <FieldText>Submitted</FieldText>
              </Field>
              <Field>
                <FieldName>BGCheck Result</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Date Hired</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Location Code</FieldName>
                <FieldText>00 Demo</FieldText>
              </Field>
            </Fields>
          </Application>
          <Application>
            <ApplicationId>6172</ApplicationId>
            <Fields>
              <Field>
                <FieldName>Action Status</FieldName>
                <FieldText>Submitted</FieldText>
              </Field>
              <Field>
                <FieldName>BGCheck Result</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Date Hired</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Location Code</FieldName>
                <FieldText>00 Demo</FieldText>
              </Field>
            </Fields>
          </Application>
        </Applications>
      </ApplicantInfo>
      <ApplicantActionDocsInfo />
      <ApplicantFormsInfo />
      <ApplicantActionsInfo />
    </Applicant>
    <Applicant>
      <ApplicantId>2196</ApplicantId>
      <ModuleTypeId>1</ModuleTypeId>
      <ApplicantInfo>
        <Applications>
          <Application>
            <ApplicationId>6184</ApplicationId>
            <Fields>
              <Field>
                <FieldName>Action Status</FieldName>
                <FieldText>Submitted</FieldText>
              </Field>
              <Field>
                <FieldName>BGCheck Result</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Date Hired</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Location Code</FieldName>
                <FieldText>Test0701</FieldText>
              </Field>
            </Fields>
          </Application>
          <Application>
            <ApplicationId>6186</ApplicationId>
            <Fields>
              <Field>
                <FieldName>Action Status</FieldName>
                <FieldText>Submitted</FieldText>
              </Field>
              <Field>
                <FieldName>BGCheck Result</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Date Hired</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Location Code</FieldName>
                <FieldText>Test0701</FieldText>
              </Field>
            </Fields>
          </Application>
          <Application>
            <ApplicationId>6188</ApplicationId>
            <Fields>
              <Field>
                <FieldName>Action Status</FieldName>
                <FieldText>Submitted</FieldText>
              </Field>
              <Field>
                <FieldName>BGCheck Result</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Date Hired</FieldName>
                <FieldText />
              </Field>
              <Field>
                <FieldName>Location Code</FieldName>
                <FieldText>Test0701</FieldText>
              </Field>
            </Fields>
          </Application>
        </Applications>
      </ApplicantInfo>
      <ApplicantActionDocsInfo />
      <ApplicantFormsInfo />
      <ApplicantActionsInfo />
    </Applicant>
  </Applicants>
</ExportedData>

正如您所看到的,有4个主要申请人,但在每个申请人中,申请人信息中都有子元素。如何格式化多个子元素以与4个主要申请人相关联以适应表格?理想情况下,我想为每个子元素添加一行,但我没有完全掌握它。

这是我当前的xsl文件:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <!-- match root - build html elements -->
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="ExportedData">
        <xsl:apply-templates/>
    </xsl:template>

    <!-- Do nothing for the header -->
    <xsl:template match="Header"/>

    <!-- Build table for applicants -->
    <xsl:template match="Applicants">
        <h2>Applicants Info</h2>
        <table border="1">
            <tr bgcolor="#9acd32">
                <th>ApplicantId</th>
                <th>ModuleTypeId</th>
                <th>ApplicantInfo</th>
            </tr>
            <xsl:apply-templates/>
        </table>
    </xsl:template>

    <!-- for each applicant create a table row -->
    <xsl:template match="Applicant">
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>

    <!-- for each Applicant/ApplicantId generate table cell -->
    <xsl:template match="ApplicantId">
        <td>
            <xsl:apply-templates/>
        </td>
    </xsl:template>

    <xsl:template match="ModuleTypeId">
        <td>
            <xsl:apply-templates/>
        </td>
    </xsl:template>

    <xsl:template match="ApplicantInfo">
        <td>
            <xsl:apply-templates/>
        </td>
    </xsl:template>

    <xsl:template match="ApplicantActionDocsInfo">
        <td>
            <xsl:apply-templates/>
        </td>
    </xsl:template>

    <xsl:template match="ApplicantFormsInfo">
        <td>
            <xsl:apply-templates/>
        </td>
    </xsl:template>

    <xsl:template match="ApplicantActionsInfo">
        <td>
            <xsl:apply-templates/>
        </td>
    </xsl:template>


    <!-- you probably will want to add more stuff to the table, use templates to do so.-->  
    <!--<xsl:template match="ModuleTypeId | ApplicantInfo | ApplicantActionDocsInfo | ApplicantFormsInfo | ApplicantActionsInfo"/>-->

</xsl:stylesheet>

同样,它正确显示了4个主要的ApplicantIds,但多个子元素未在表中正确显示。如何生成额外的行以实现此目的?

更新

我的输出目前是这样的:http://i.imgur.com/vfntoV3.png

我希望它是这样的:http://i.imgur.com/tAD88c1.png

2 个答案:

答案 0 :(得分:1)

  

我希望每个ApplicationId都有一行

如果是这样,您不应为每个申请人创建一行,而是为每个申请人创建一行。我也不明白为什么你需要这样一片模板 - 它几乎无法管理。

尝试改为:

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:template match="/">
    <html>
        <body>
            <h2>Applicants Info</h2>
            <table border="1">
            <tr bgcolor="#9acd32">
                <th>ApplicantId</th>
                <th>ModuleTypeId</th>
                <th>ApplicantInfo</th>
            </tr>
                <xsl:apply-templates select="ExportedData/Applicants/Applicant/ApplicantInfo/Applications/Application"/>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="Application">
    <tr>
        <td>
            <xsl:value-of select="ancestor::Applicant/ApplicantId"/>
        </td>
        <td>
            <xsl:value-of select="ancestor::Applicant/ModuleTypeId"/>
        </td>
        <td>
            <xsl:value-of select="."/>
        </td>
    </tr>
</xsl:template>

</xsl:stylesheet>

应用于输入时的结果(呈现):

enter image description here

当然标签Applicants Info用词不当:它实际上是Application Info

答案 1 :(得分:0)

假设您要为其他一些元素添加其他列,这将显示如何使用通用匹配并使用@mode为标题列生成不同的内容。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <!-- match root - build html elements -->
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates select="ExportedData/Applicants"/>
            </body>
        </html>
    </xsl:template>

    <!-- Build table for applicants -->
    <xsl:template match="Applicants">
        <h2>Applicants Info</h2>
        <table border="1">
            <tr bgcolor="#9acd32">
                <xsl:apply-templates select="Applicant[1]/*" mode="header"/>
            </tr>
            <xsl:apply-templates select="Applicant/ApplicantInfo/Applications/Application"/>
        </table>
    </xsl:template>

    <!--Either add more specific elements, or change to * if you want headers for each of the Applicant child elements-->
    <xsl:template match="ApplicantId | ModuleTypeId | ApplicantInfo" mode="header">
        <th><xsl:value-of select="local-name()"/></th>
    </xsl:template>

    <!-- for each application create a table row -->
    <xsl:template match="Application">
        <tr>
            <xsl:apply-templates select="ancestor::ApplicantInfo/preceding-sibling::*"/>
            <td><xsl:apply-templates/></td>
            <!--uncomment below, if you want to add colums for the elements after ApplicantInfo, such as ApplicantActionDocsInfoo, ApplicantFormsInfo, ApplicatnActionsInfo-->
            <!--<xsl:apply-templates select="ancestor::ApplicantInfo/following-sibling::*"/>-->
        </tr>
    </xsl:template>

    <!-- for each Applicant child element generate table cell -->
    <xsl:template match="Applicant/*">
        <td>
            <xsl:apply-templates/>
        </td>
    </xsl:template>

</xsl:stylesheet>