0

I have an xml output file that I want to check that a directory exists:

<row_element column="6" property_name="TEST">file_name = sample.txt ~ parent_file = main.txt ~ root_directory = c:\main\sample</row_element>
<row_element column="6" property_name="TEST">file_name = sample2.txt ~ parent_file = main.txt ~ root_directory = c:\main\sample2</row_element>

I tried the code below, but noticed that the substring-before with the '~' does not seem to work due to the fact that my xml data does not end with a '~'. Is there another method to do this check or at least tweak this code to work?

<xsl:variable name="find_root" select="row_element[@property_name = 'TEST'"/>
<xsl:if test="(normalize-space(substring-before(substring-after($find_root/., 'file_name ='), '~')) = 'sample.txt') and (normalize-space(substring-before(substring-after($find_root/., 'parent_file ='), '~')) = 'main.txt') and (normalize-space(substring-before(substring-after($find_root/., 'root_directory ='), '~')) = 'c:\main\sample')">
<xsl:value-of select="'FOUND'"/>
</xsl:if>

Thanks in advance.

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by xml_looser
0

for testing

<?xml version="1.0"?>
<root>
	<row_element column="6" property_name="TEST">file_name = sample.txt ~ parent_file = main.txt ~ root_directory = c:\main\sample</row_element>
	<row_element column="6" property_name="TEST">file_name = sample2.txt ~ parent_file = main.txt</row_element>
	<row_element column="6" property_name="TEST">file_name = sample.txt ~ parent_file = main.txt ~ root_directory = c:\main\sample</row_element>
	<row_element column="6" property_name="TEST">file_name = sample2.txt ~ parent_file = main.txt ~ root_directory = c:\main\sample2</row_element>
</root>

xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output indent="yes" method="xml"/>
	<xsl:template match="/">
		<root>
			<xsl:apply-templates select="root"/>
		</root>
	</xsl:template>
	<xsl:template match="root">
		<xsl:apply-templates select="row_element"/>
	</xsl:template>
	<xsl:template match="row_element">
		<xsl:variable name="find_root" select=".[@property_name = 'TEST']"/>
		<item>
			<xsl:variable name="sub1" select="concat(normalize-space(substring-before(substring-after($find_root/., 'file_name ='), 'txt')),'txt')"/>
			<xsl:variable name="sub2" select="concat(normalize-space(substring-before(substring-after($find_root/., 'parent_file ='), 'txt')),'txt')"/>
			<xsl:variable name="sub3" select="normalize-space(substring-after($find_root/., 'root_directory ='))"/>

			<var>
				<xsl:value-of select="$find_root"/>
			</var>
			<!-- -->
			<sub1>
				<!--
				<xsl:value-of select="concat(normalize-space(substring-before(substring-after($find_root/., 'file_name ='), 'txt')),'txt')"/>
				-->
				<xsl:value-of select="$sub1"/>
			</sub1>
			<sub2>
				<!--
				<xsl:value-of select="concat(normalize-space(substring-before(substring-after($find_root/., 'parent_file ='), 'txt')),'txt')"/>
				-->
				<xsl:value-of select="$sub2"/>
			</sub2>
			<sub3>
				<!--
				<xsl:value-of select="normalize-space(substring-after($find_root/., 'root_directory ='))"/>
				-->
				<xsl:value-of select="$sub3"/>
			</sub3>
			<result>
				<xsl:choose>
					<xsl:when test="($sub1 = 'sample.txt') and ($sub2 = 'main.txt') and ($sub3 = 'c:\main\sample')">
						<xsl:text>FOUND</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>NOT FOUND</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</result>
		</item>
	</xsl:template>
</xsl:stylesheet>

result

<?xml version='1.0' ?>
<root>
  <item>
    <var>file_name = sample.txt ~ parent_file = main.txt ~ root_directory = c:\main\sample</var>
    <sub1>sample.txt</sub1>
    <sub2>main.txt</sub2>
    <sub3>c:\main\sample</sub3>
    <result>FOUND</result>
  </item>
  <item>
    <var>file_name = sample2.txt ~ parent_file = main.txt</var>
    <sub1>sample2.txt</sub1>
    <sub2>main.txt</sub2>
    <sub3/>
    <result>NOT FOUND</result>
  </item>
  <item>
    <var>file_name = sample.txt ~ parent_file = main.txt ~ root_directory = c:\main\sample</var>
    <sub1>sample.txt</sub1>
    <sub2>main.txt</sub2>
    <sub3>c:\main\sample</sub3>
    <result>FOUND</result>
  </item>
  <item>
    <var>file_name = sample2.txt ~ parent_file = main.txt ~ root_directory = c:\main\sample2</var>
    <sub1>sample2.txt</sub1>
    <sub2>main.txt</sub2>
    <sub3>c:\main\sample2</sub3>
    <result>NOT FOUND</result>
  </item>
</root>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.