【问题标题】:XSL - How to check if current node has no text and if true add textXSL - 如何检查当前节点是否没有文本以及是否为真添加文本
【发布时间】:2014-06-28 12:07:36
【问题描述】:

如果找到属性 1234 和 boolean1 值,则以下代码将 Y 或 N 分配给元素 ABC。 但有时属性 id 1234 根本不存在于 /Response/Specification/Section/item/Property 中。 所以元素 ABC 在输出 xml 中保持为空。 当属性 1234 不存在时,我想将 Element 值设置为 N。 我对 XSL 和 XML 很陌生。

输入 XML,其中每个规范标签包含多个部分和项目。每个部分和项目都有自己的 id。

<Response> <Specification> <Section> <sectionid>9999</sectionid> <item> <itemid>8888</itemid> <property> <id>1234</id> <property>ABC<property> <boolean1>1</boolean1> </property> <property> <id>5678</id> <property>XYZ<property> <boolean1>0</boolean1> </property> </property> <property> </property> </item> <item> </item> <item> </item> </Section> <Section> </Section> <Section>
</Section> </Specification> </Response>

格式良好的输入 xml 的小样本。

<Response>
<Specification>
    <Section>
        <Sectionid>9999</Sectionid>
            <Item>                
                <Itemid>8888</Itemid>
        <Property>
                        <id>1234</id>
                    <Property>ABC</Property>
                        <Boolean1>1</Boolean1>
                </Property>
                <Property>
                        <id>5678</id>
                        <Property>XYZ</Property>
                        <Boolean1>0</Boolean1>
                </Property>                
            </Item>
</Section>     
</Specification>
</Response>

在原始帖子中,我删除了一些元素。我虽然它会使它变得简短而简单。下面是所有元素的更新 xsl

<ABC>
<xsl:for-each select="Response/Specification/Section/Item/Property[id=1234]">
    <xsl:choose>
        <xsl:when test="boolean1=1" >
            <xsl:text>Y</xsl:text>
        </xsl:when>                 
        <xsl:otherwise>
            <xsl:text>N</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>
</ABC> 
<XYZ>
<xsl:for-each select="Response/Specification/Section/Item/Property[id=5678]">
    <xsl:choose>
        <xsl:when test="boolean1=1" >
            <xsl:text>Y</xsl:text>
        </xsl:when>                 
        <xsl:otherwise>
            <xsl:text>N</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>
</XYZ> 

预期的输出 XML 仅有 parenttag,其中包含所有其他标签,如 ABC、XYZ、PQR 等。当属性 id 1234 不存在时,输出为空 ABC 标签。这里我们要添加 N 而不是空标签。

<parenttag>
</ABC>
<XYZ>N</XYZ>
</parenttag>  

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    如果你有多个 property(或者它是 Property?记住 XML 和 XSLT,区分大小写),我将从迭代 >property 元素,但此时不检查任何值

    <xsl:for-each select="Response/Specification/Section/item/property"> 
    

    在此范围内,如果您想创建一个名称基于子 property 元素的元素,您可以将 xsl:element 与属性值模板一起使用动态指定元素名称。

    <xsl:element name="{property}">   
    

    然后,在此范围内,您可以在测试条件时使用 xsl:choose,包括对“1234”的检查

    <xsl:choose>
        <xsl:when test="ID=1234 and BOOLEAN1=1" >
            <xsl:text>Y</xsl:text>
        </xsl:when>         
        <xsl:otherwise>
            <xsl:text>N</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
    

    试试这个 XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
       <xsl:template match="/">
          <parenttag>
             <xsl:for-each select="Response/Specification/Section/Item/Property">
                <xsl:element name="{Property}">
                   <xsl:choose>
                      <xsl:when test="id=1234 and Boolean1=1">
                         <xsl:text>Y</xsl:text>
                      </xsl:when>
                      <xsl:otherwise>
                         <xsl:text>N</xsl:text>
                      </xsl:otherwise>
                   </xsl:choose>
                </xsl:element>
             </xsl:for-each>
          </parenttag>
       </xsl:template>
    </xsl:stylesheet>
    

    编辑:如果元素名称取决于 ID,并且您希望它们始终存在,您可以这样做:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
       <xsl:template match="/">
          <parenttag>
             <ABC>
                <xsl:choose>
                   <xsl:when test="Response/Specification/Section/Item/Property[id=1234]/Boolean1=1">
                      <xsl:text>Y</xsl:text>
                   </xsl:when>
                   <xsl:otherwise>
                      <xsl:text>N</xsl:text>
                   </xsl:otherwise>
                </xsl:choose>
             </ABC>
             <XYZ>
                <xsl:choose>
                   <xsl:when test="Response/Specification/Section/Item/Property[id=5678]/Boolean1=1">
                      <xsl:text>Y</xsl:text>
                   </xsl:when>
                   <xsl:otherwise>
                      <xsl:text>N</xsl:text>
                   </xsl:otherwise>
                </xsl:choose>
             </XYZ>
          </parenttag>
       </xsl:template>
    </xsl:stylesheet>
    

    不过,为了避免代码重复,您可以使用命名模板。试试这个

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
    
       <xsl:template match="/">
          <parenttag>
             <ABC>
                <xsl:call-template name="check">
                   <xsl:with-param name="id" select="'1234'"/>
                </xsl:call-template>
             </ABC>
             <XYZ>
                <xsl:call-template name="check">
                   <xsl:with-param name="id" select="'5678'"/>
                </xsl:call-template>
             </XYZ>
          </parenttag>
       </xsl:template>
    
       <xsl:template name="check">
          <xsl:param name="id"/>
          <xsl:choose>
             <xsl:when test="Response/Specification/Section/Item/Property[id=$id]/Boolean1=1">
                <xsl:text>Y</xsl:text>
             </xsl:when>
             <xsl:otherwise>
                <xsl:text>N</xsl:text>
             </xsl:otherwise>
          </xsl:choose>
       </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 嗨蒂姆,是的,我们有 50 个左右的多个属性。每个元素只包含 Y 或 N 的文本。在上面的解决方案中尝试过,它给出了 Y 和 N 的字符串,正如你所说的。但我们只想要 Y 或 N。你能指导我如何实现这一目标吗?
    • 如果您显示输入 XML 的示例以及您期望的输出,这将有很大帮助。谢谢。
    • 嗨蒂姆,我已经用输入 xml、输出 xml 和更新的 xsl 更新了帖子。如果这些信息有帮助,请告诉我。谢谢。
    • 您能确保输入和输出 XML 格式正确吗?另外,PQR 是从哪里来的,因为我在输入 XML 的任何地方都看不到?谢谢。
    • 嗨,蒂姆,我添加了 PQR 只是举例。抱歉,将其从输出中删除。谢谢。
    猜你喜欢
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多