小编Joh*_*Xsl的帖子

根据其他元素的值删除元素——XSLT

我有一个样式表,用于根据其他元素的值删除某些元素。但是,它不起作用......

示例输入 XML

<Model>
<Year>1999</Year>
<Operation>ABC</Operation>
<Text>Testing</Text>
<Status>Ok</Status>
</Model>
Run Code Online (Sandbox Code Playgroud)

如果操作值为“ABC”,则从 XML 中删除文本和状态节点。并给出以下输出。

<Model>
<Year>1999</Year>
<Operation>ABC</Operation>
</Model>
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的样式表,但即使操作不是“ABC”,它也会从所有 XML 中删除文本和状态节点。

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:variable name="ID" select="//Operation"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="Text | Status">
    <xsl:if test ="$ID ='ABC'">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

提前致谢

当命名空间存在时,我将如何做同样的事情

<ns0:next type="Sale" xmlns:ns0="http://Test.Schemas.Inside_Sales">
Run Code Online (Sandbox Code Playgroud)

xslt nodes

2
推荐指数
1
解决办法
8449
查看次数

XSLT XML到XML转换,验证,动态创建节点/元素

我正在使用以下XSLT将XML转换为XML.我需要验证所需元素的源XML.如果所需节点缺少兄弟节点的值,则创建一个新节点.这是XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <Data Schema="XML A">
            <xsl:apply-templates/>
        </Data>
    </xsl:template>
    <xsl:template match="Attribute[not(Type=following::Type)]">
        <Attributes type="{Type}">
            <xsl:apply-templates 
                 select="../Attribute[Type=current()/Type]" mode="out"/>
        </Attributes>
    </xsl:template>
    <xsl:template match="Attribute" mode="out">
        <Attr id="{id}" name="{Name}" value="{Value}"/>
    </xsl:template>
    <xsl:template match="Attribute"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

这是XML

<?xml version="1.0" encoding="windows-1252"?>
<XML>
    <Attributes>
        <Attribute>
            <id>331</id>
            <Name>Enviornment</Name>
            <Type>common</Type>
            <Value>Development</Value>
        </Attribute>
        <Attribute>
            <id>79</id>
            <Name>Retail</Name>
            <Type>common</Type>
            <Value></Value>
        </Attribute>
        <Attribute>
            <id>402</id>
            <Name>Gender</Name>
            <Type>category</Type>
            <Value>Men</Value>
        </Attribute>
    </Attributes>
</XML>
Run Code Online (Sandbox Code Playgroud)

如果缺少必需的元素,那么它应该创建以下XML.我有多个必需的元素.

<?xml version="1.0" encoding="utf-8"?>
<Data Schema="XML A">
  <Attributes type="common">
    <Attr id="331" name="Enviornment" value="Development" />
    <Attr id="79" name="Retail" value="" />
  </Attributes>
  <Attributes type="category"> …
Run Code Online (Sandbox Code Playgroud)

xml xslt validation required

1
推荐指数
1
解决办法
2846
查看次数

Xpath,count属性为null的节点

我有一个以下的XML.

<root>
    <a>
    </a>
    <a b="ar34" >
    </a>
    <a c="utr45">
    </a>
</root>
Run Code Online (Sandbox Code Playgroud)

我想进行不存在属性"b"的计数.我使用以下但没有得到计数

count(//*[string-length(a/@b) = 0]
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?谢谢

xpath

1
推荐指数
1
解决办法
3180
查看次数

标签 统计

xslt ×2

nodes ×1

required ×1

validation ×1

xml ×1

xpath ×1