【问题标题】:Need help in generating text output from XML file在从 XML 文件生成文本输出时需要帮助
【发布时间】:2019-01-24 19:52:03
【问题描述】:

在构建 XSL 2.0 版文件以将 XML 文件转换为文本输出时需要帮助。

以下是 XML 文件。对于所有字段为“Checked = 'Y'”的元素,我们需要在文本输出之前连接一个“[X]”。

制表位应基于 ansestor-tags 的级别。

<Review>
	<Assignment/>
	<Authorization/>
	<Criteria>
		<Components/>
		<OrganizationalPolicy/>
		<DecisionPoints/>
		<Notes/>
		<QualityIndicators/>
		<Responses>
			<Response CPID="AISD01590403010101" Checked="Y"/>
			<Response CPID="AISD01590403010102" Checked="Y"/>
			<Response CPID="AISD01590403010103" Checked="Y"/>
			<Response CPID="AISD0159040301010401" Checked="Y"/>
			<Response CPID="AISD01590403010104" Met="Y"/>
			<Response CPID="AISD015904030101" Met="Y"/>
			<Response CPID="AISD0159040301010402" Checked="Y"/>
			<Response CPID="AISD0159040301010403" Checked="Y"/>
			<Response CPID="AISD0159040301010404" Met="Y"/>
			<Response CPID="AISD015904030101040401" Checked="Y"/>
			<Response CPID="AISD015904030101040402" Checked="Y"/>
			<Response CPID="AISD015904030101040403" Checked="Y"/>
			<Response CPID="AISD015904030101040404" Checked="Y"/>
			<Response CPID="AISD0159040301010405" Checked="Y"/>
		</Responses>
		<QuestionsAsked/>
	</Criteria>
	<ReviewSummaryXML>
		<CP ID="AISD015901" Txt="(Symptom or finding within 24h)"/>
		<CP ID="AISD015902" Txt="(Excludes PO medications unless noted)">
			<CP ID="AISD015904" Txt="Select Day, One:">
				<CP ID="AISD01590401" Txt="Pre-op Day, One:"/>
				<CP ID="AISD01590402" Txt="Operative Day, One:"/>
				<CP ID="AISD01590403" Txt="Post-op Day 1, One:">
					<CP ID="AISD0159040301" Txt="OBSERVATION, One:">
						<CP Checked="Y" ID="AISD015904030101" Txt="Responder, discharge expected today if clinically stable last 12h, All:">
							<CP Checked="Y" ID="AISD01590403010101" Txt="Able to perform ADLs or return to baseline"/>
							<CP Checked="Y" ID="AISD01590403010102" Txt="Pain controlled or manageable"/>
							<CP Checked="Y" ID="AISD01590403010103" Txt="Tolerating PO or nutritional route established"/>
							<CP Checked="Y" ID="AISD01590403010104" Txt="Complication or comorbidity, &gt;= One:">
								<CP Checked="Y" ID="AISD0159040301010401" Txt="No complication or active comorbidity relevant to this episode of care"/>
								<CP Checked="Y" ID="AISD0159040301010402" Txt="Arrhythmia controlled"/>
								<CP Checked="Y" ID="AISD0159040301010403" Txt="Bleeding controlled or manageable"/>
								<CP Checked="Y" ID="AISD0159040301010404" Txt="Recovered from anesthesia, All:">
									<CP Checked="Y" ID="AISD015904030101040401" Txt="Stable level of consciousness"/>
									<CP Checked="Y" ID="AISD015904030101040402" Txt="Mobility and coordination at baseline"/>
									<CP Checked="Y" ID="AISD015904030101040403" Txt="Sensation intact"/>
									<CP Checked="Y" ID="AISD015904030101040404" Txt="O2 sat &gt;= 92%(0.92) or within acceptable limits"/>
								</CP>
								<CP Checked="Y" ID="AISD0159040301010405" Txt="Passing urine without urinary retention"/>
								<CP ID="AISD0159040301010406" Txt="Postoperative vomiting resolved"/>
							</CP>
						</CP>
					</CP>
				</CP>
				<CP ID="AISD01590404" Txt="Post-op Day 2, One:"/>
				<CP ID="AISD01590405" Txt="Post-op Day 3, One:"/>
				<CP ID="AISD01590406" Txt="Post-op Day 4, One:"/>
				<CP ID="AISD01590407" Txt="Post-op Day 5, One:"/>
				<CP ID="AISD01590408" Txt="Post-op Day 6-10, One:"/>
				<CP ID="AISD01590409" Txt="Post-op Day 11, One:"/>
			</CP>
		</CP>
	</ReviewSummaryXML>
</Review>

预期输出如下。

(Excludes PO medications unless noted)
	Select Day, One:
		Post-op Day 1, One
			OBSERVATION, One:
				[X] Responder, discharge expected today if clinically stable last 12h, All:
					[X] Able to perform ADLs or return to baseline
					[X] Pain controlled or manageable
					[X]	Tolerating PO or nutritional route established
					[X] Complication or comorbidity, &gt;= One:
						[X]	No complication or active comorbidity relevant to this episode of care
						[X]	Arrhythmia controlled
						[X] Bleeding controlled or manageable
						[X] Recovered from anesthesia, All:
							[X]	Stable level of consciousness
							[X] Mobility and coordination at baseline
							[X] Sensation intact
							[X] O2 sat &gt;= 92%(0.92) or within acceptable limits
								[X] Passing urine without urinary retention

【问题讨论】:

  • 请问您目前有什么代码?
  • @AnyMoose 这是我到目前为止所拥有的。 xsltransform.net/aiwQ4n/1
  • 您能否编辑您的问题以包含 XSLT? xsltransform.net 经常不可用....谢谢

标签: xml xslt transformation ancestor


【解决方案1】:

在您的 XSLT 中,您似乎只选择 Checked 为“Y”的元素

<xsl:for-each select="descendant-or-self::*[@Checked='Y']">

因此,所有其他元素都将被忽略。我认为您应该在这里选择具有Txt 属性的元素:

 <xsl:for-each select="descendant-or-self::*[@Txt]">

(计数也是如此)

那么,要确定是否在前面显示“[X]”,可以这样做:

<xsl:if test="@Checked='Y'">[X] </xsl:if>
<xsl:value-of select="@Txt"/>

或者这样:

<xsl:value-of select="concat(if (@Checked = 'Y') then '[X] ' else '', @Txt)"/>

试试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output indent="yes" method="text"/>

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

    <xsl:template match="ReviewSummaryXML">
        <xsl:for-each select="descendant-or-self::*[@Txt]">
            <xsl:call-template name="tab">
                <xsl:with-param name="ancestor-count" select="count(ancestor::*[@Txt])"/>
            </xsl:call-template>
            <xsl:value-of select="concat(if (@Checked = 'Y') then '[X] ' else '', @Txt)"/><xsl:text>&#x0a;</xsl:text>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="tab">
        <xsl:param name="ancestor-count"/>
        <xsl:for-each select="1 to $ancestor-count">
            <xsl:text>&#x9;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

还要注意xsl:strip-space 的使用,以避免在文本之前输出过多的空格。

编辑 - 在回答您的评论时,如果您只想输出 @Checked 为“Y”的元素,或者它们的后代元素的 @Checked 等于“Y”,请添加条件[descendant-or-self::*/@Checked='Y'] 到相关表达式。

试试这个 XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output indent="yes" method="text"/>

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

    <xsl:template match="ReviewSummaryXML">
        <xsl:for-each select="descendant-or-self::*[@Txt][descendant-or-self::*/@Checked='Y']">
            <xsl:call-template name="tab">
                <xsl:with-param name="ancestor-count" select="count(ancestor::*[@Txt][descendant-or-self::*/@Checked='Y'])"/>
            </xsl:call-template>
            <xsl:value-of select="concat(if (@Checked = 'Y') then '[X] ' else '', @Txt)"/><xsl:text>&#x0a;</xsl:text>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="tab">
        <xsl:param name="ancestor-count"/>
        <xsl:for-each select="1 to $ancestor-count">
            <xsl:text>&#x9;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

编辑 2:如果您想要 XSLT 1.0 解决方案,试试这个....

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">

    <xsl:output indent="yes" method="text"/>

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

    <xsl:template match="ReviewSummaryXML">
        <xsl:for-each select="descendant-or-self::*[@Txt][descendant-or-self::*/@Checked='Y']">
            <xsl:call-template name="tab">
                <xsl:with-param name="ancestors" select="ancestor::*[@Txt][descendant-or-self::*/@Checked='Y']"/>
            </xsl:call-template>
            <xsl:if test="@Checked = 'Y'">[X] </xsl:if>
            <xsl:value-of select="@Txt"/>
            <xsl:text>&#x0a;</xsl:text>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="tab">
        <xsl:param name="ancestors"/>
        <xsl:for-each select="$ancestors">
            <xsl:text>&#x9;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

  • 谢谢,@TImC,上面的 XSLT 文件也生成了所有没有至少一个 Checked='y' 的节点。我们只需要获取至少有一个子节点的祖先节点的参数为 Checked = 'Y'。
  • @naidunp - 我已经用如何实现这一点的示例修改了我的答案。
  • 当我试图在 XML 记事本或微软的 xslt 解析器中添加 XSLT 文件时,它给了我一个错误,说“表达式预期结束,找到 'to '。”我们如何减轻这种情况。
  • 这是一个 XSLT 2.0 解决方案。几乎可以肯定,任何用于 XSLT 的 Mircosoft 工具都是 XSLT 1.0。
  • 谢谢,@TimC 有没有办法实现这些功能来实现输出。
猜你喜欢
  • 2022-11-13
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多