Java 复制引用的XML元素
我有一个源XML文档,看起来像这样:Java 复制引用的XML元素,java,xml,xslt,Java,Xml,Xslt,我有一个源XML文档,看起来像这样: <root> <users> <user id="1" name="user 1" /> <user id="2" name="user 2" /> <user id="3" name="user 3" /> </users> <posts> <post>
<root>
<users>
<user id="1" name="user 1" />
<user id="2" name="user 2" />
<user id="3" name="user 3" />
</users>
<posts>
<post>
<user>1</user>
<text>First sample post!</text>
<status>DELETED</status>
</post>
<post>
<user>2</user>
<text>Second sample post!</text>
<status>ACTIVE</status>
</post>
<post>
<user>3</user>
<text>Third sample post!</text>
<status>DELETED</status>
</post>
</posts>
</root>
1.
第一个样本职位!
删除
2.
第二个样本职位!
活跃的
3.
第三个样本职位!
删除
我需要筛选用户,以便目标文档仅包含活动帖子和在帖子元素中引用的用户:
<root>
<users>
<user id="2" name="user 2" />
</users>
<posts>
<post>
<user>2</user>
<text>Second sample post!</text>
</post>
</posts>
</root>
2.
第二个样本职位!
我没有更改源文档的权限,我需要使用XSLT(我对XSLT非常陌生)使之成为可能
我可以很容易地过滤帖子,但我不知道如何建立用户列表
在我进一步讨论之前,我想检查一下这是否可行
干杯是的,这是可能的,样式表如下:
<root>
<users>
<user id="1" name="user 1" />
<user id="2" name="user 2" />
<user id="3" name="user 3" />
</users>
<posts>
<post>
<user>1</user>
<text>First sample post!</text>
<status>DELETED</status>
</post>
<post>
<user>2</user>
<text>Second sample post!</text>
<status>ACTIVE</status>
</post>
<post>
<user>3</user>
<text>Third sample post!</text>
<status>DELETED</status>
</post>
</posts>
</root>
试验
TransformerFactory工厂=TransformerFactory.newInstance();
Transformer Transformer=factory.newTransformer(新的StreamSource(新文件(“test.xslt”));
setOutputProperty(OutputKeys.OMIT_XML_声明,“yes”);
transformer.transform(新的StreamSource(新文件(“test.xml”),
新的StreamResult(System.out));
输出
2.
第二个样本职位!
是的,有可能使用如下样式表:
<root>
<users>
<user id="1" name="user 1" />
<user id="2" name="user 2" />
<user id="3" name="user 3" />
</users>
<posts>
<post>
<user>1</user>
<text>First sample post!</text>
<status>DELETED</status>
</post>
<post>
<user>2</user>
<text>Second sample post!</text>
<status>ACTIVE</status>
</post>
<post>
<user>3</user>
<text>Third sample post!</text>
<status>DELETED</status>
</post>
</posts>
</root>
试验
TransformerFactory工厂=TransformerFactory.newInstance();
Transformer Transformer=factory.newTransformer(新的StreamSource(新文件(“test.xslt”));
setOutputProperty(OutputKeys.OMIT_XML_声明,“yes”);
transformer.transform(新的StreamSource(新文件(“test.xml”),
新的StreamResult(System.out));
输出
2.
第二个样本职位!
首先,您应该了解身份模板
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
类似地,用于删除状态
节点本身(对于它复制的帖子)
然后,忽略用户的模板将是
<xsl:template match="user[key('posts', @id)/status!='ACTIVE']" />
把这些放在一起给你这个
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="posts" match="post" use="user" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="post[status!='ACTIVE']" />
<xsl:template match="status" />
<xsl:template match="user[key('posts', @id)/status!='ACTIVE']" />
</xsl:stylesheet>
首先,您应该了解身份模板
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
类似地,用于删除状态
节点本身(对于它复制的帖子)
然后,忽略用户的模板将是
<xsl:template match="user[key('posts', @id)/status!='ACTIVE']" />
把这些放在一起给你这个
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="posts" match="post" use="user" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="post[status!='ACTIVE']" />
<xsl:template match="status" />
<xsl:template match="user[key('posts', @id)/status!='ACTIVE']" />
</xsl:stylesheet>
问题确实说了“仅活动”,所以可能是状态!='如果示例中没有显示其他状态,则“活动”状态“
将优于“状态=”已删除”。哎呀!也解决了这个问题。非常感谢。问题确实说了“仅活动”,所以可能状态!='如果示例中没有显示其他状态,则“活动”状态“
将优于“状态=”已删除”。哎呀!也解决了这个问题。非常感谢。