如果不满足匹配条件,XSLT 创建空文档
XSLT create empty document if match criteria is not met
如果不符合我的标准城市和 还有 select 一些特定元素,我想得到一个空文档。要求是仅转换某些特定元素,并且仅首选子元素。意味着不是所有的元素都应该被复制
这是我的示例 XML 文档。
<?xml version="1.0" encoding="utf-8"?>
<Class xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" TimeStamp="2020-06-20T16:15:01.9906696+07:00" Version="9.1" Sequence="827170" xmlns="http://localhost/2007/00">
<ID>001</ID>
<Student>
<Profile>
<Name>G1</Name>
<City>PNH</City>
<RegDate>2020-06-20</RegDate>
</Profile>
<Origin>
<Address>
<City>REP</City>
</Address>
</Origin>
</Student>
<Student>
<Profile>
<Name>G4</Name>
<City>REP</City>
<RegDate>2020-06-20</RegDate>
</Profile>
<Origin>
<Address>
<City>PNH</City>
</Address>
</Origin>
</Student>
</Class>
下面是我尝试的 xslt 示例
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:ns0="http://localhost/2007/00">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns0:Student[not(ns0:Profile/ns0:City = 'PNH')]" />
<!--select Profile from student only -->
<xsl:template match="ns0:Student">
<xsl:copy>
<xsl:apply-templates select="ns0:Profile"/>
</xsl:copy>
</xsl:template>
<!-- select City and RegDate from Profile only -->
<xsl:template match="ns0:Profile">
<xsl:copy>
<xsl:apply-templates select="ns0:City|ns0:RegDate"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如您所见,我想找到个人资料 > 城市等于 KOS 的学生
如果没有找到学生,我想处理 return 空字符串。但是在 运行 之后,它在上下文中得到 <Class>
和 <ID>
而不是预期的空字符串。
如果城市不符合我的条件,您是否介意协助如何获取空字符串作为结果?
这是我希望看到的城市是否符合我的标准(学生>个人资料>城市='PNH')
<Class xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://localhost/2007/00" TimeStamp="2020-06-20T16:15:01.9906696+07:00" Version="9.1" Sequence="827170">
<ID>001</ID>
<Student>
<Profile>
<City>PNH</City>
<RegDate>2020-06-20</RegDate>
</Profile>
</Student>
</Class>
如果城市不匹配则只有空字符串 return。
--- 根据问题的变化进行编辑 ---
要满足增加的要求,您需要采用不同的方法:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://localhost/2007/00">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/ns0:Class">
<xsl:variable name="stu" select="ns0:Student[ns0:Profile/ns0:City='KOS']" />
<xsl:if test="$stu">
<xsl:copy>
<xsl:copy-of select="@* | ns0:ID"/>
<xsl:apply-templates select="$stu"/>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="ns0:Student">
<xsl:copy>
<xsl:apply-templates select="ns0:Profile"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns0:Profile">
<xsl:copy>
<xsl:copy-of select="ns0:City|ns0:RegDate"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如果不符合我的标准城市和 还有 select 一些特定元素,我想得到一个空文档。要求是仅转换某些特定元素,并且仅首选子元素。意味着不是所有的元素都应该被复制
这是我的示例 XML 文档。
<?xml version="1.0" encoding="utf-8"?>
<Class xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" TimeStamp="2020-06-20T16:15:01.9906696+07:00" Version="9.1" Sequence="827170" xmlns="http://localhost/2007/00">
<ID>001</ID>
<Student>
<Profile>
<Name>G1</Name>
<City>PNH</City>
<RegDate>2020-06-20</RegDate>
</Profile>
<Origin>
<Address>
<City>REP</City>
</Address>
</Origin>
</Student>
<Student>
<Profile>
<Name>G4</Name>
<City>REP</City>
<RegDate>2020-06-20</RegDate>
</Profile>
<Origin>
<Address>
<City>PNH</City>
</Address>
</Origin>
</Student>
</Class>
下面是我尝试的 xslt 示例
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:ns0="http://localhost/2007/00">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns0:Student[not(ns0:Profile/ns0:City = 'PNH')]" />
<!--select Profile from student only -->
<xsl:template match="ns0:Student">
<xsl:copy>
<xsl:apply-templates select="ns0:Profile"/>
</xsl:copy>
</xsl:template>
<!-- select City and RegDate from Profile only -->
<xsl:template match="ns0:Profile">
<xsl:copy>
<xsl:apply-templates select="ns0:City|ns0:RegDate"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如您所见,我想找到个人资料 > 城市等于 KOS 的学生
如果没有找到学生,我想处理 return 空字符串。但是在 运行 之后,它在上下文中得到 <Class>
和 <ID>
而不是预期的空字符串。
如果城市不符合我的条件,您是否介意协助如何获取空字符串作为结果?
这是我希望看到的城市是否符合我的标准(学生>个人资料>城市='PNH')
<Class xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://localhost/2007/00" TimeStamp="2020-06-20T16:15:01.9906696+07:00" Version="9.1" Sequence="827170">
<ID>001</ID>
<Student>
<Profile>
<City>PNH</City>
<RegDate>2020-06-20</RegDate>
</Profile>
</Student>
</Class>
如果城市不匹配则只有空字符串 return。
--- 根据问题的变化进行编辑 ---
要满足增加的要求,您需要采用不同的方法:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://localhost/2007/00">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/ns0:Class">
<xsl:variable name="stu" select="ns0:Student[ns0:Profile/ns0:City='KOS']" />
<xsl:if test="$stu">
<xsl:copy>
<xsl:copy-of select="@* | ns0:ID"/>
<xsl:apply-templates select="$stu"/>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="ns0:Student">
<xsl:copy>
<xsl:apply-templates select="ns0:Profile"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns0:Profile">
<xsl:copy>
<xsl:copy-of select="ns0:City|ns0:RegDate"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>