简体   繁体   English

将字符串解析为XML,然后将xPath解析为XML-XSLT 2或3

[英]Parse string to XML and xPath into it - XSLT 2 or 3

Answers can be XSLT 2.0 or 3.0; 答案可以是XSLT 2.0或3.0; Saxon EE parser Saxon EE解析器

Given XML sample: 给定的XML示例:

<theRootNode xmlns="http://myNamespace">
    <xml>
        &lt;Company&gt;
            &lt;TaxIdentity&gt;
                &lt;id/&gt;
            &lt;/TaxIdentity&gt;
            &lt;TaxIdentity&gt;
                &lt;id&gt;42-123456&lt;/id&gt;
            &lt;/TaxIdentity&gt;
            &lt;Name&gt;ABC Company&lt;/Name&gt;
            &lt;Number&gt;2230&lt;/Number&gt;
        &lt;/Company&gt;
    </xml>
</theRootNode>

I have a 2.0 XSLT stylesheet which removes the namespaces and parses the xml node. 我有一个2.0 XSLT样式表,它删除了名称空间并解析了xml节点。 Within the same transform I now need to remove any 'TaxIdentity' nodes which have empty values within the xml node I just parsed. 在同一个转换中,我现在需要删除所有“ TaxIdentity”节点,这些节点在我刚解析的xml节点中具有空值。

Current results are: 当前结果是:

<theRootNode>
    <xml>
        <Company>
            <TaxIdentity>
                <id/>
            </TaxIdentity>
            <TaxIdentity>
                <id>42-123456</id>
            </TaxIdentity>
            <Name>ABC Company</Name>
            <Number>2230</Number>
        </Company>
    </xml>
</theRootNode>

Desired results are: 所需的结果是:

<theRootNode>
    <xml>
        <Company>
            <TaxIdentity>
                <id>42-123456</id>
            </TaxIdentity>
            <Name>ABC Company</Name>
            <Number>2230</Number>
        </Company>
    </xml>
</theRootNode>

My XSLT is below. 我的XSLT在下面。 Note that my thought was to put the initial results into a variable using as="element() or use parse-xml (version 3) but I can't seem remove the empty nodes. Note that my goal is NOT to remove all empty elements or nodes...just ones named TaxIdentiy. What approach should I take? Solutions? I really need it all within the same transform. Thanks. 请注意,我的想法是使用as =“ element()或使用parse-xml(版本3)将初始结果放入变量中,但我似乎无法删除空节点。请注意,我的目标不是删除所有空节点。元素或节点...只是命名为TaxIdentiy的元素或节点,我应该采用哪种方法?解决方案?我真的需要在同一转换中全部使用它们。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no" omit-xml-declaration="yes"/>
    <!-- Copy elements without copying their namespace declarations -->
    <xsl:template match="*[not(local-name() = 'xml')]" name="identity">
        <xsl:element name="{name()}" namespace="">
            <xsl:apply-templates select="node() | @*"/>
        </xsl:element>
    </xsl:template>
    <!-- Copy content as is with lower priority -->
    <xsl:template match="node() | @*" priority="-2">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <!-- turn xml node content back into xml -->
    <xsl:template match="*[local-name() = 'xml']">
        <xsl:element name="xml" inherit-namespaces="no" namespace="">
            <xsl:value-of select="." disable-output-escaping='yes'/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Here is my XSLT 3.0 suggestion which simply parses the XML with parse-xml and then apply-templates to the parsed nodes with a template removing the TaxIdentity with all empty child elements: 这是我的XSLT 3.0建议,该建议仅使用parse-xml ,然后使用删除所有空子元素的TaxIdentity模板将模板应用于已解析的节点:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:is="http://myNamespace"
    exclude-result-prefixes="is xs math"
    version="3.0">

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="is:*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* , node()"></xsl:apply-templates>
        </xsl:element>
    </xsl:template>

    <xsl:template match="is:xml">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="parse-xml(.)/node()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="TaxIdentity[not(*[normalize-space()])]"/>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM