如何根据复杂条件显示元素的属性?

时间:2015-09-01 20:39:09

标签: xml xslt

我有一个xml文档,我想在Number元素中显示<CrossReferenceNumber>属性 在我的xslt中,Number位于<ext:SupersededProtectionOrderID>元素内。

Xml文档可以有<Type Op="A" Word="SBJO">Signed By Judicial Officer</Type>

<Type Op="A" Word="SBJO">Signed By Judicial Officer – Corrected’</Type>

以下是条件:

条件一

如果ProtectionOrder/CrossReferenceNumbers/CrossReferenceNumber/Type = ‘OFP System Number’

ProtectionOrders/ProtectionOrder/Statuses/Status/Type = Signed By Judicial Officer有一个Op = “A”

显示

xslt中ProtectionOrder/CrossReferenceNumbers/CrossReferenceNumber/Number元素中的

ext:SupersededProtectionOrderID

条件二

如果ProtectionOrder/CrossReferenceNumbers/CrossReferenceNumber/Type = ‘OFP System Number’

ProtectionOrders/ProtectionOrder/Statuses/Status/Type = ‘Signed By Judicial Officer – Corrected’

并且有Op = ‘A’

显示

ProtectionOrder/CrossReferenceNumbers/CrossReferenceNumber/Number

在xsl的ext:SupersededProtectionOrderID元素中。

期望的输出 <ext:SupersededProtectionOrderID>OFP System Number</ext:SupersededProtectionOrderID>

我的xml

<Integration xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:tsg="http://tsgweb.com" xmlns:IXML="http://tsgweb.com" xmlns:CMCodeQueryHelper="urn:CMCodeQueryHelper" PackageID="BCA PO Notification" MessageID="67177928" xmlns="">
    <ProtectionOrders>
        <ProtectionOrder Op="E" InternalProtectionOrderID="932">
            <ProtectionOrderNumber>201500845</ProtectionOrderNumber>
            <Type Word="OFP">Order for Protection</Type>
            <Statuses>
                <Status Op="A">
                    <Current>true</Current>
                    <Active>Yes</Active>
                    <Date Op="A">09/01/2015</Date>
                    <Type Op="A" Word="SBJO">Signed By Judicial Officer</Type>
                    <TimestampCreate Op="A">09/01/2015 12:39:49:763</TimestampCreate>
                </Status>
                <Status>
                    <Current>false</Current>
                    <Active>No</Active>
                    <Date>09/01/2015</Date>
                    <Type Word="DRAFT">Draft</Type>
                    <TimestampCreate>09/01/2015 12:39:43:207</TimestampCreate>
                </Status>
            </Statuses>
            <CrossReferenceNumbers>
                <CrossReferenceNumber>
                    <Type Word="NCICNUM">NCIC Number</Type>
                    <Number>Testing123</Number>
                </CrossReferenceNumber>
            </CrossReferenceNumbers>
            <TimestampCreate>09/01/2015 12:39:43:140</TimestampCreate>
            <TimestampChange>09/01/2015 12:39:49:750</TimestampChange>
        </ProtectionOrder>
    </ProtectionOrders>
</Case>
<ProtectionOrder Op="E" InternalProtectionOrderID="932" xmlns:user="http://tylertechnologies.com">
    <ProtectionOrderNumber>201500845</ProtectionOrderNumber>
    <Type Word="OFP">Order for Protection</Type>
    <Statuses>
        <Status Op="A">
            <Current>true</Current>
            <Active>Yes</Active>
            <Date Op="A">09/01/2015</Date>
            <Type Op="A" Word="SBJO">Signed By Judicial Officer</Type>
            <TimestampCreate Op="A">09/01/2015 12:39:49:763</TimestampCreate>
        </Status>
        <Status>
            <Current>false</Current>
            <Active>No</Active>
            <Date>09/01/2015</Date>
            <Type Word="DRAFT">Draft</Type>
            <TimestampCreate>09/01/2015 12:39:43:207</TimestampCreate>
        </Status>
    </Statuses>
    <CrossReferenceNumbers>
        <CrossReferenceNumber>
            <Type Word="NCICNUM">OFP System Number</Type>
            <Number>Testing123</Number>
        </CrossReferenceNumber>
    </CrossReferenceNumbers>
</ProtectionOrder>

我的xslt代码

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:exc="http://www.courts.state.mn.us/ProtectionOrderServiceExchange/1.0" xmlns:ext="http://www.courts.state.mn.us/ProtectionOrderExtension/1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template name="ProtectionOrder">
        <!--SupersededProtectionOrderID-->
        <ext:SupersededProtectionOrderID>
        <xsl:choose>
            <xsl:when test="(Statuses/Status/Type/@Op='A') and count(CrossReferenceNumbers/CrossReferenceNumber/Type='OFP System Number')>0">
            <xsl:value-of select="Number"/>
            </xsl:when>
        <xsl:otherwise>
        <xsl:value-of select="/Integration/ProtectionOrder[@InternalProtectionOrderID=current()/MNProtectionOrderAdditional/@SupersededProtectionOrderID]/ProtectionOrderNumber"/>
        </xsl:otherwise>
        </xsl:choose>
        </ext:SupersededProtectionOrderID>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

注意:在您的要求中,您解释何时输出<Number>,但这不是属性,如标题中所述。

替换它:

<ext:SupersededProtectionOrderID>
<xsl:choose>
    <xsl:when test="(Statuses/Status/Type/@Op='A') and count(CrossReferenceNumbers/CrossReferenceNumber/Type='OFP System Number')>0">
    <xsl:value-of select="Number"/>
    </xsl:when>
<xsl:otherwise>
<xsl:value-of select="/Integration/ProtectionOrder[@InternalProtectionOrderID=current()/MNProtectionOrderAdditional/@SupersededProtectionOrderID]/ProtectionOrderNumber"/>
</xsl:otherwise>
</xsl:choose>
</ext:SupersededProtectionOrderID>

有了这个:

<!-- assuming your named template has the correct context item! -->
<xsl:apply-templates select="Statuses/Status/Type" mode="number" />

并添加:

<xsl:template mode="number" match="Type
    [@Op = 'A'][
       . = 'Signed By Judicial Officer'
       or . = 'Signed By Judicial Officer - Corrected'
    ]">
    <xsl:apply-templates mode="number"
        select="../../../CrossReferenceNumbers
        /CrossReferenceNumber[Type='OFP System Number'][1]" />
</xsl:template>

<xsl:template match="CrossReferenceNumber" mode="number">
    <ext:SupersededProtectionOrderID>
        <xsl:value-of select="Number"/>
    </ext:SupersededProtectionOrderID>
</xsl:template>

这将符合您的第一个要求。使用具有依赖于元素数量的条件的应用模板,或者存在一个元素通常比使用xsl:ifxsl:choose更容易,如果不仅因为您获得了焦点的优势和背景变化。此外,无论您的输入尺寸,其他元素,位置或深度如何,它都能正常工作。

编辑:编辑以适应您的条件。