检查发生的XSLT

时间:2015-01-12 04:37:40

标签: xml xslt

我想检查某些文字的出现。下面是我的XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>

<xsl:template match="/">

<!-- Notify PANTONE - If YES -->
<xsl:if test="contains(xmlreport/PageInfo/PageAttribute/PageColor,'PANTONE')">
<PantonePage><note>Yes</note></PantonePage>
</xsl:if>

<!-- Notify PANTONE - If NO -->
<xsl:if test="not(contains(xmlreport/PageInfo/PageAttribute/PageColor,'PANTONE'))">
<PantonePage><note>No</note></PantonePage>
</xsl:if>

</xsl:template>
</xsl:stylesheet>

我的愿望输出应该是:

<pantone>Yes</pantone>

<pantone>No</pantone>

在原始XML中,PANTONE可能是PANTONE Red U,PANTONE Green U.在测试上述XSLT时,答案始终为NO。

以下是我的XML:

<?xml version="1.0" encoding="UTF-8"?>
<xmlreport>
  <PageInfo>
    <PageAttribute>
      <PageNum>1</PageNum>
      <TrimSize>
        <H>220 mm</H>
        <W>150 mm</W>
      </TrimSize>
      <MediaSize>
        <H>225 mm</H>
        <W>160 mm</W>
      </MediaSize>
      <PageColor>PANTONE Red U</PageColor>
    </PageAttribute>
    <PageAttribute>
      <PageNum>2</PageNum>
      <TrimSize>
        <H>220 mm</H>
        <W>150 mm</W>
      </TrimSize>
      <MediaSize>
        <H>225 mm</H>
        <W>160 mm</W>
      </MediaSize>
      <PageColor>Black</PageColor>
    </PageAttribute>
    <PageAttribute>
      <PageNum>3</PageNum>
      <TrimSize>
        <H>220 mm</H>
        <W>150 mm</W>
      </TrimSize>
      <MediaSize>
        <H>225 mm</H>
        <W>160 mm</W>
      </MediaSize>
      <PageColor>Cyan Magenta Yellow Black</PageColor>
    </PageAttribute>
  <PageInfo>
<xmlreport>

PageAttribute将根据页数重复。

2 个答案:

答案 0 :(得分:2)

您的尝试不起作用的原因是contains()是一个字符串函数和表达式:

contains(xmlreport/PageInfo/PageAttribute/PageColor,'PANTONE')"

仅测试集合的第一个节点的字符串值。

尝试改为:

<xsl:template match="/">
    <PantonePage>
        <note>
            <xsl:choose>
                <xsl:when test="xmlreport/PageInfo/PageAttribute[contains(PageColor, 'PANTONE')]">Yes</xsl:when>
                <xsl:otherwise>No</xsl:otherwise>
            </xsl:choose>
        </note>
    </PantonePage>
</xsl:template>

或 - 如果您可以接受真/假的结果而不是是/否 - 只需:

<xsl:template match="/">
    <PantonePage>
        <note>
            <xsl:value-of select="boolean(xmlreport/PageInfo/PageAttribute[contains(PageColor, 'PANTONE')])"/>
        </note>
    </PantonePage>
</xsl:template>

答案 1 :(得分:0)

根据提供的示例xml

编辑样式表

我现在更改了方法,因为您更清楚地知道业务规则,您希望页面信息元素中有多个页面属性。如果任何一个页面是pantone并且完全正确的话,迈克尔的回答肯定会导致单个值返回。以下方法的不同之处在于提供了一种结构来识别哪些页面是Pantone,以防其他偶然发现这些答案的人正在按页面查找该业务规则。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>  
    <xsl:template match="/">
        <results>
            <xsl:apply-templates />
        </results>
    </xsl:template>
    <xsl:template match="xmlreport/PageInfo/PageAttribute">
        <Page>
            <PageNum><xsl:number/></PageNum>
            <Pantone>
                <xsl:choose>
                    <xsl:when test="contains(PageColor,'PANTONE')">Yes</xsl:when>
                    <xsl:otherwise>No</xsl:otherwise>
                </xsl:choose>
            </Pantone>
        </Page>
    </xsl:template>
</xsl:stylesheet>

针对您提供的示例XML运行此产品:

<results>
    <Page>
        <PageNum>1</PageNum>
        <Pantone>Yes</Pantone>
    </Page>
    <Page>
        <PageNum>2</PageNum>
        <Pantone>No</Pantone>
    </Page>
    <Page>
        <PageNum>3</PageNum>
        <Pantone>No</Pantone>
    </Page>
</results>
相关问题