【问题标题】:XSLT: For each with conditionXSLT:对于每个有条件
【发布时间】:2021-07-28 09:16:45
【问题描述】:

目前,当我通过 XSLT 转换 XML 时,它为所有节点生成,但我想根据以下条件输出

对于任何节点,当Reason_Code = AA 检查Auth1 的值并将其带到前一个节点并捕获同一节点中其他标签的值。请指教

输入 XML:

<?xml version='1.0' encoding='utf-8'?>
<ns0:File xmlns:ns0="http://yourcompany.com/textschema/mapping/item.xsd">
    <Detail>
        <empty1>75500013</empty1>
        <empty2>00001N</empty2>
        <Reason_Code>B6</Reason_Code>
    </Detail>
    <Detail>
        <empty1>75500012</empty1>
        <empty2>000011</empty2>
        <Reason_Code>AA</Reason_Code>
    </Detail>
    <Detail>
        <empty1>4453252243</empty1>
        <empty2>0004235</empty2>
        <Auth1>AAA</Auth1>
        <Reason_Code>AA</Reason_Code>
    </Detail>
    <Detail>
        <empty1>75500016</empty1>
        <empty2>00002N</empty2>
        <Reason_Code>B3</Reason_Code>
    </Detail>
    <Detail>
        <empty1>75500018</empty1>
        <empty2>000014</empty2>
        <Reason_Code>AA</Reason_Code>
    </Detail>
    <Detail>
        <empty1>43524433</empty1>
        <empty2>000415</empty2>
        <Auth1>BBB</Auth1>
        <Reason_Code>AA</Reason_Code>
    </Detail>
</ns0:File>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns0="http://yourcompany.com/textschema/mapping/item.xsd">
    <xsl:output method="xml" indent="yes" omit-xml-declaration = "yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/">
        <Items_All>
            <xsl:for-each select="ns0:File/Detail">
                <Item>
                    
                    
                    <Item_ID>
                        <xsl:value-of select="empty1"/>
                    </Item_ID>
                    
                    <File_ID>
                        <xsl:value-of select="empty2"/>
                    </File_ID>
                    
                    <Auth>
                        <xsl:value-of select="Auth1"/>
                    </Auth>
                    
                    <Reason_Code>
                        <xsl:value-of select="Reason_Code"/>
                    </Reason_Code>
                    
                </Item>
                
            </xsl:for-each>
        </Items_All>
    </xsl:template>
</xsl:stylesheet>

生成的输出:

<Items_All xmlns:ns0="http://yourcompany.com/textschema/mapping/item.xsd">
   <Item>
      <Item_ID>75500013</Item_ID>
      <File_ID>00001N</File_ID>
      <Auth/>
      <Reason_Code>B6</Reason_Code>
   </Item>
   <Item>
      <Item_ID>75500012</Item_ID>
      <File_ID>000011</File_ID>
      <Auth/>
      <Reason_Code>AA</Reason_Code>
   </Item>
   <Item>
      <Item_ID>4453252243</Item_ID>
      <File_ID>0004235</File_ID>
      <Auth>AAA</Auth>
      <Reason_Code>AA</Reason_Code>
   </Item>
   <Item>
      <Item_ID>75500016</Item_ID>
      <File_ID>00002N</File_ID>
      <Auth/>
      <Reason_Code>B3</Reason_Code>
   </Item>
   <Item>
      <Item_ID>75500018</Item_ID>
      <File_ID>000014</File_ID>
      <Auth/>
      <Reason_Code>AA</Reason_Code>
   </Item>
   <Item>
      <Item_ID>43524433</Item_ID>
      <File_ID>000415</File_ID>
      <Auth>BBB</Auth>
      <Reason_Code>AA</Reason_Code>
   </Item>
</Items_All>

所需的输出:

<Items_All xmlns:ns0="http://yourcompany.com/textschema/mapping/item.xsd">
    <Item>
        <Item_ID>75500013</Item_ID>
        <File_ID>00001N</File_ID>
        <Auth/>
        <Reason_Code>B6</Reason_Code>
    </Item>
    <Item>
        <Item_ID>75500012</Item_ID>
        <File_ID>000011</File_ID>
        <Auth>AAA</Auth>
        <Reason_Code>AA</Reason_Code>
    </Item>
    <Item>
        <Item_ID>75500016</Item_ID>
        <File_ID>00002N</File_ID>
        <Auth/>
        <Reason_Code>B3</Reason_Code>
    </Item>
    <Item>
        <Item_ID>75500018</Item_ID>
        <File_ID>000014</File_ID>
        <Auth>BBB</Auth>
        <Reason_Code>AA</Reason_Code>
    </Item>
</Items_All>

目前,当我通过 XSLT 转换 XML 时,它为所有节点生成,但我想根据以下条件输出

对于任何节点,当Reason_Code = AA 检查Auth1 的值并将其带到前一个节点并捕获同一节点中其他标签的值。请指教

【问题讨论】:

  • 您似乎有四个Detail 元素,其中Reason_Code 在您的输入样本中具有AA 的值。您的口头描述“当 Reason_Code = AA 检查 Auth1 的值并将其带到前一个节点并捕获同一节点中其他标签的值”并没有清楚地解释哪些节点必须合并。那是Reason_CodeAA 的任何相邻Detail 元素组吗?你可以使用 XSLT 2 或 3 吗?

标签: xml xslt


【解决方案1】:

尝试以下方法以获得您想要的结果:

如果您的意思是将匹配的Auth1 值复制到上一个节点并抑制匹配的Detail 元素

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://yourcompany.com/textschema/mapping/item.xsd">
    <xsl:output method="xml" indent="yes" omit-xml-declaration = "yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/">
        <Items_All>
            <xsl:apply-templates/>
        </Items_All>
    </xsl:template>
    
    <xsl:template match="Detail">
        <Items>
            <Item_ID>
                <xsl:value-of select="empty1"/>
            </Item_ID>
                    
            <File_ID>
                <xsl:value-of select="empty2"/>
            </File_ID>
            
            <Auth>
                <xsl:value-of select="following-sibling::*[1][name()]/Auth1"/>
            </Auth>
            
            <Reason_Code>
                <xsl:value-of select="Reason_Code"/>
            </Reason_Code>
        </Items>
    </xsl:template>
    
    <xsl:template match="Detail[Reason_Code='AA' and Auth1]"/>
    
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 2020-05-06
    • 2020-06-19
    • 2011-05-03
    • 1970-01-01
    相关资源
    最近更新 更多