如果缺少元素,请检查XSLT元素和输入空间

如果缺少元素,请检查XSLT元素和输入空间,xslt,xslt-2.0,element,xslt-3.0,Xslt,Xslt 2.0,Element,Xslt 3.0,我正在转换一些元素不会显示的传入XML。若元素并没有数据,那个么那个特定的元素就不会出现。在下面的示例中,我总共有4个XML报告组,其中每个组的预期标记为EmployeeId、first_name、last_name、job_code、活动的总共5个标记。然而,对于少数群体,一些或其他元素将丢失在我需要放置空间的地方。对于报告组1和3,所有元素都存在,报告条目2的姓氏缺失,对于报告条目4,活动和姓氏缺失 当我写入O/p时,我希望确保在缺少任何元素时输入空格,否则会产生数据问题、列标题和列数据不匹

我正在转换一些元素不会显示的传入XML。若元素并没有数据,那个么那个特定的元素就不会出现。在下面的示例中,我总共有4个XML报告组,其中每个组的预期标记为EmployeeId、first_name、last_name、job_code、活动的总共5个标记。然而,对于少数群体,一些或其他元素将丢失在我需要放置空间的地方。对于报告组1和3,所有元素都存在,报告条目2的姓氏缺失,对于报告条目4,活动和姓氏缺失

当我写入O/p时,我希望确保在缺少任何元素时输入空格,否则会产生数据问题、列标题和列数据不匹配问题

<?xml version="1.0" encoding="UTF-8"?>
<master>
<report_group>
    <employeeid>05121</employeeid>
    <first_name>John</first_name>
    <last_name>Blad</last_name>
    <job_code>0001</job_code>
   <active>Y</active>
</report_group>
<report_group>
    <employeeid>05671</employeeid>
    <first_name>Cris</first_name>
    <job_code>0002</job_code>
    <active>N</active>     
</report_group>
<report_group>
    <employeeid>05432</employeeid>
    <first_name>Vel</first_name>
    <last_name>Harris</last_name>
    <job_code>0004</job_code>
    <active>Y</active>
</report_group>
<report_group>
    <employeeid>05672</employeeid>
    <last_name>vens</last_name>
    <job_code>789</job_code>
</report_group>
有谁面临过这类问题?有谁知道如何处理这种情况


非常感谢您提供的任何帮助

您有两种选择,如果选择是空序列,则将子元素选择馈送到输出空格的函数:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mf="http://example.com/mf"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="3.0">

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

  <xsl:function name="mf:space-if-empty" as="xs:string">
      <xsl:param name="input" as="item()?"/>
      <xsl:sequence select="if (empty($input)) then ' ' else string($input)"/>
  </xsl:function>

  <xsl:template match="report_group">
      <xsl:value-of select="mf:space-if-empty(employeeid), mf:space-if-empty(first_name), mf:space-if-empty(last_name), mf:space-if-empty(job_code), mf:space-if-empty(active)" separator=","/>
      <xsl:text>&#10;</xsl:text>
  </xsl:template>

</xsl:stylesheet>



这是一个在线示例

或者通过一种模式运行输入,该模式将缺少的元素添加到空格中:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mf="http://example.com/mf"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs mf"
    expand-text="yes"
    version="3.0">

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

  <xsl:mode name="add-dummy-elements" on-no-match="shallow-copy"/>

  <xsl:template match="report_group" mode="add-dummy-elements">
      <xsl:copy>
          <employeeid>{mf:space-if-empty(employeeid)}</employeeid>
          <first_name>{mf:space-if-empty(first_name)}</first_name>
          <last_name>{mf:space-if-empty(last_name)}</last_name>
          <job_code>{mf:space-if-empty(job_code)}</job_code>
          <active>{mf:space-if-empty(active)}</active>
      </xsl:copy>
  </xsl:template>

  <xsl:function name="mf:space-if-empty" as="xs:string">
      <xsl:param name="input" as="item()?"/>
      <xsl:sequence select="if (empty($input)) then ' ' else string($input)"/>
  </xsl:function>

  <xsl:variable name="input-with-dummies-added">
      <xsl:apply-templates mode="add-dummy-elements"/>
  </xsl:variable>

  <xsl:template match="/">
      <xsl:text>id,fname,lname,jbcode,active&#10;</xsl:text>
      <xsl:apply-templates select="$input-with-dummies-added/node()"/>
  </xsl:template>

  <xsl:template match="report_group">
      <xsl:value-of select="employeeid, first_name, last_name, job_code, active" separator=","/>
      <xsl:text>&#10;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

{mf:employeeid为空时的空格}
{mf:如果为空,则为空格(名字)}
{mf:如果为空,则为空格(姓氏)}
{mf:空时空格(作业代码)}

这说明了这一点。

谢谢Martin,我采用了第一种方法,它确实帮了我大忙
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mf="http://example.com/mf"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs mf"
    expand-text="yes"
    version="3.0">

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

  <xsl:mode name="add-dummy-elements" on-no-match="shallow-copy"/>

  <xsl:template match="report_group" mode="add-dummy-elements">
      <xsl:copy>
          <employeeid>{mf:space-if-empty(employeeid)}</employeeid>
          <first_name>{mf:space-if-empty(first_name)}</first_name>
          <last_name>{mf:space-if-empty(last_name)}</last_name>
          <job_code>{mf:space-if-empty(job_code)}</job_code>
          <active>{mf:space-if-empty(active)}</active>
      </xsl:copy>
  </xsl:template>

  <xsl:function name="mf:space-if-empty" as="xs:string">
      <xsl:param name="input" as="item()?"/>
      <xsl:sequence select="if (empty($input)) then ' ' else string($input)"/>
  </xsl:function>

  <xsl:variable name="input-with-dummies-added">
      <xsl:apply-templates mode="add-dummy-elements"/>
  </xsl:variable>

  <xsl:template match="/">
      <xsl:text>id,fname,lname,jbcode,active&#10;</xsl:text>
      <xsl:apply-templates select="$input-with-dummies-added/node()"/>
  </xsl:template>

  <xsl:template match="report_group">
      <xsl:value-of select="employeeid, first_name, last_name, job_code, active" separator=","/>
      <xsl:text>&#10;</xsl:text>
  </xsl:template>

</xsl:stylesheet>