【问题标题】:Stripping tags from xml while preserving contained text and parent tags using xslt从 xml 中剥离标签,同时使用 xslt 保留包含的文本和父标签
【发布时间】:2013-06-06 04:44:03
【问题描述】:

我正在尝试从一些 XML 中剥离标签,如下所示:

<vocabularyModel>
<conceptDomain name="ActAccountType">
    <annotations>
        <documentation>
            <definition>
                <text>
                    <p>
                        <b>Description: </b>more txt here </p>
                    <p>
                        <i>Examples: </i>
                    </p>
                    <p/>
                    <ul>
                        <li>
                            <p>Patient billing accounts</p>
                        </li>
                        <li>
                            <p>Cost center</p>
                        </li>
                        <li>
                            <p>Cash</p>
                        </li>
                    </ul>
                </text>
            </definition>
        </documentation>
    </annotations>
</conceptDomain>
<conceptDomain name="ActAdjudicationInformationCode">
    <annotations>
        <documentation>
            <definition>
                <text>
                    <p>long text.</p>
                    <p>long text.</p>
                    <p>long text.</p>
                    <p>long text.</p>
                </text>
            </definition>
        </documentation>
    </annotations>
</conceptDomain>
<conceptDomain name="ActAdjudicationType">
    <annotations>
        <documentation>
            <definition>
                <text>
                    <p>
                        <b>Description: </b>more text.</p>
                    <p>
                        <i>Examples: </i>
                    </p>
                    <p/>
                    <ul>
                        <li>
                            <p>adjudicated with adjustments</p>
                        </li>
                        <li>
                            <p>adjudicated as refused</p>
                        </li>
                        <li>
                            <p>adjudicated as submitted</p>
                        </li>
                    </ul>
                </text>
            </definition>
        </documentation>
    </annotations>
</conceptDomain>

文本下方的所有子标签都将被剥离,但所需的 xml 和文本如下所示:

<vocabularyModel>
<conceptDomain name="ActAccountType">
    <annotations>
        <documentation>
            <definition>
                <text>
                   Description: more txt here 
                        Examples: 
                          Patient billing accounts
                          Cost center
                          Cash
                </text>
            </definition>
        </documentation>
    </annotations>
</conceptDomain>
<conceptDomain name="ActAdjudicationInformationCode">
    <annotations>
        <documentation>
            <definition>
                <text>
                    long text.
                    long text.
                    long text.
                    long text.
                </text>
            </definition>
        </documentation>
    </annotations>>
</conceptDomain>
<conceptDomain name="ActAdjudicationReason">
    <annotations>
        <documentation>
            <definition>
                <text>
                    long text.
                    long text.
                    long text.
                    long text.
                </text>
            </definition>
        </documentation>
    </annotations>
    <specializesDomain name="ActReason"/>
</conceptDomain>
<conceptDomain name="ActAdjudicationType">
    <annotations>
        <documentation>
            <definition>
                <text>
                        Description: more text.
                        Examples: 
                            adjudicated with adjustments
                            adjudicated as refused
                            adjudicated as submitted
                </text>
            </definition>
        </documentation>
    </annotations>
</conceptDomain>

我已尝试在此处其他地方找到并修改了以下内容:

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="p | b | li | ul | i">
    <xsl:apply-templates/>
</xsl:template>

但这并没有去除任何元素,即使我将匹配限制在元素上。我还尝试了以下几种变体:

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


<xsl:template name="strip-tags">
    <xsl:param name="html"/>
    <xsl:choose>
        <xsl:when test="contains($html, '&lt;')">
            <xsl:value-of select="substring-before($html, '&lt;')"/>
            <xsl:call-template name="strip-tags">
                <xsl:with-param name="html" select="substring-after($html, '&gt;')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$html"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>


<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="definition">
    <xsl:call-template name="strip-tags">
        <xsl:with-param name="html" select="text"/>
    </xsl:call-template>
</xsl:template>

如果我省略了身份转换,这将删除所有标签,但否则只会复制原始 XML 的内容。任何帮助都感激不尽。 -斯科特

【问题讨论】:

    标签: xml xslt strip-tags


    【解决方案1】:

    您显示的第一个样式表的输出(如果您添加缺少的xsl:stylesheet 元素是

    <vocabularyModel>
       <conceptDomain name="ActAccountType">
          <annotations>
             <documentation>
                <definition>
                   <text>Description: more txt here Examples: Patient billing accountsCost centerCash</text>
                </definition>
             </documentation>
          </annotations>
       </conceptDomain>
       <conceptDomain name="ActAdjudicationInformationCode">
          <annotations>
             <documentation>
                <definition>
                   <text>long text.long text.long text.long text.</text>
                </definition>
             </documentation>
          </annotations>
       </conceptDomain>
       <conceptDomain name="ActAdjudicationType">
          <annotations>
             <documentation>
                <definition>
                   <text>Description: more text.Examples: adjudicated with adjustmentsadjudicated as refusedadjudicated as submitted</text>
                </definition>
             </documentation>
          </annotations>
       </conceptDomain>
    </vocabularyModel>
    

    这似乎是您想要的。也许您真正的输入是在命名空间中?

    【讨论】:

    • 莫名其妙。实际的根包含一些属性,并且在它之前有几个概念域的兄弟姐妹,但其他方面如图所示。
    • 一旦我去掉 xmlns 名称,它就可以正常工作。我还不知道如何在 xslt 中执行此操作,但我会四处寻找解决方案。感谢您的观看。
    • @hsb 在样式表中使用前缀(比如x)声明命名空间,然后将h="p | b | li | ul | i" 更改为h="x:p | x:b | x:li | x:ul | x:i",以便匹配命名空间中的元素而不是无命名空间。
    • 在这里显示我的无知。也许我不想剥离命名空间,只是以某种方式解释此处声明的默认命名空间 xmlns="urn:hl7-org:v3/mif2" 以某种方式。
    • 谢谢,我看到您期待我的回复。再次感谢您的观看。
    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多